I have a @OneToMany relationship defined on the parent class like so:
public class Course {
@OneToMany(
mappedBy = "courseId",
fetch = FetchType.EAGER,
cascade = CascadeType.ALL,
orphanRemoval = true)
private Set<Student> students;
}
On the other side of the relationship, I simply keep the id of the parent entity:
public class Student {
private Long courseId;
}
When I save a Course with new Students, hibernate first persists the Course, and then tries to persist each Student which is what I would expect. (I can see this via hibernate logging.)
However, when it goes to insert each Student, it is passing a null
for the courseId
. The database ends up throwing this error:
ERROR: null value in column "courseid" violates not-null constraint
I have other examples of this working correctly in the code, but for some reason this one is behaving differently.
Is there a reason it's not using the id from the Course it just saved? Is there some other configuration I need to add to support this?