Hibernate: OneToMany save children by cascade
Your Parent.class try A seems already correct. But to cascade the child while saving the parent, you must put the cascade on the owner side (in one-to-many, it is the entity that have Foreign Key).
Try this
@Entity
public class Parent {
@OneToMany(mappedBy = "parent")
protected List<Child> children;
}
and in your Child.class
@Entity
public class Child {
@ManyToOne
@Cascade(value={org.hibernate.annotations.CascadeType.ALL})
@JoinColumn(name="PARENT_ID")
protected Parent parent;
}
I guess if you answer the question from the first comment, we will come to this situation:
- you have an already persisted parent
- you have new child objects, that were not yet persisted
- you add the children to the parent and do saveOrUpdate
In that case hibernate just cascades the save or update to the children, but they cannot be saved or updated as they have not been persistent yet. And now Hibernate says simply "I cannot update a non persistent entity"
In one sentence: Hibernate only cascades what it is issued to cascade. In this case you issue a "SAVE_UPDATE", which is cascaded then further to the children. I guess you expected Hibernate to be smart and switch to persist for the children here. But this is not how Hibernate works here, I came across similar situations before, too. A bit confusing, but if you once got how cascading works, you will see such situations.
Try this:
@Entity
@Table(name = "TABLE_PARENT")
public class Parent{
@OneToMany(mappedBy="parent", cascade=CascadeType.PERSIST)
private List<Child> children;
}
@Entity
@Table(name = "TABLE_CHILD")
public class Child{
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="PARENT_ID")
private Parent parent;
}
Usage
public void save(){
Parent parent = new Parent();
List<Child> children = new ArrayList<>();
Child child = new Child();
child.setParent(parent);
children.add(child);
parent.setChildren(children);
parentRepository.save(parent);
}
Note: Don't forget to set the parent on child object or you will get TABLE_CHILD.PARENT_ID null/empty