OiO.lk Blog java CascadeType.ALL doesn't save the children. the child doesn't get presist and saved
java

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.



You need to sign in to view this answers

Exit mobile version