When I persist a question instance, the answer objects related to that question does not set the foreign key to the primary key of the question. However, both the question instance and all answers belonging to it are persisted to the database successfully.
If you need more code I will add it.
Question entity
@OneToMany(mappedBy = "question", fetch = FetchType.EAGER, cascade = { CascadeType.ALL })
@OrderBy("serialNumber ASC")
private List<Answer> answers;
Answer entity
@ManyToOne(fetch = FetchType.LAZY)
private Question question;
Or do I manually have assign the question instance to each answer through the setmethod?
Update
Okey, I tried setting the question instance throught the setter on each answer object, and it works. But, can somebody say if that is correct when you use cascade rules as above. You still need to manually wire them together.
Yes, it’s correct. Hibernate uses the owning side of the association to persist the association. The owning side is the side where there is no “mappedBy” attribute.
The cascade will make all the answers persistent when you persist a question, and will merge the answers when you merge the question, and similarly for all the other operations. But the association must be handled by you.