JPA @OneToMany -> Parent - Child Reference (Foreign Key)

It still seems to be the case. In parent Entity you can have something like

@PrePersist
private void prePersist() {
   children.forEach( c -> c.setParent(this));
}

in order to avoid repeating code for setting child/parent relationship elsewhere in code.


Do I really have to do sth. like this?

That is one strategy, yes.

On bi-directional relationships there is an "owning" and a "non-owning" side of the relationship. Because the owning side in your case is on Child, you need to set the relationship there for it to be persisted. The owning side is usually determined by where you specify @JoinColumn, but it doesn't look like you're using that annotation, so it's likely being inferred from the fact that you used mappedBy in the Parent annotation.

You can read a lot more about this here.


Yes, that is the case. JPA does not keep care about consistency of your entity graph. Especially you have to set it to the owner side of bidirectional relationship (in your case to the parent attribute of Child).

In JPA 2.0 specification this is said with following words:

Note that it is the application that bears responsibility for maintaining the consistency of run- time relationships—for example, for insuring that the “one” and the “many” sides of a bidi- rectional relationship are consistent with one another when the application updates the relationship at runtime.