CascadeType.ALL doesn’t save the children. the child doesn’t get presist and saved

I have three Entity such as following:

@Table(name = "input")
@Entity
@Data
public class CreationInputEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    @Column(name = "ID", unique = true, nullable = false, updatable = false)
    Long id;

    @OneToOne(cascade=CascadeType.ALL)
    @JoinColumn(name = "index_id", referencedColumnName = "id")
    IndexEntity index;
}

@Table(name = "index")
@Entity
@Data
public class IndexEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    @Column(name = "id", unique = true, nullable = false, updatable = false)
    Long id;

    @OneToOne(mappedBy = "index")
    CreationInputEntity creationInputEntity;

    @NotBlank
    @Column(name = "indexName")
    String indexName;

    @OneToMany(mappedBy = "index", cascade=CascadeType.ALL)
    List<IndexshareEntity> indexshares;
}

@Entity
@Data
public class IndexshareEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    @Column(name = "ID", unique = true, nullable = false, updatable = false)
    public Long id;

....

    @ManyToOne
    @JoinColumn(name="index_id")
    public IndexEntity index;
}

and I have several endpoints. When in first create endpoint, I create the CreationInputEntity endpoint, and saveAndFlush, it shows in database and with debugging that all part of it such as IndexEntity and IndexshareEntity got saved. but after saving and trying to fetch the data with the getRequest, there is a IndexEntity, but it’s IndexshareEntity is empty and the IndexshareEntities didn’t remained saved in the database.

CreationInputEntity createdCreationInputEntity = inputRepository.saveAndFlush(creationInputEntity);

Where am I doing wrong? why the children does’t stay in the database? I thought saveAndFlush with CascadeType.ALL would be enough for saving all parts of the entity?

Thank you in advanced for your helps.

4

Your case is explained by the fact that at the time of saving, the child IndexshareEntity objects do not contain a reference to the parent IndexEntity object. The relationship between IndexEntity and IndexshareEntity is managed by the IndexshareEntity object, since that is where you joined the @JoinColumn(name="index_id") column. However, at the time of saving, IndexshareEntity objects do not contain a reference to the IndexEntity object, so the IndexEntity object id is not stored in the index_id column. Therefore, when you load the IndexEntity, Hibernate cannot determine the IndexshareEntity objects that are related to it.

To solve the problem, you must set a reference to the parent object for each child object. This can be done either manually using the IndexshareEntity.setIndex() method, or automatically using the @JsonManagedReference and @JsonBackReference annotations.

class IndexEntity {

  @JsonManagedReference
  @OneToMany(mappedBy = "index", cascade=CascadeType.ALL)
  List<IndexshareEntity> indexshares;
}

class IndexshareEntity {

  @ManyToOne
  @JsonBackReference
  @JoinColumn(name="index_id")
  public IndexEntity index;
}

Unfortunately, judging by your code, the @JsonManagedReference and @JsonBackReference annotations won’t help you.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật