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

后端 未结 5 1832
太阳男子
太阳男子 2020-12-07 19:10

i have a Question about referencing ParentEntities from Child Entites ir If i have something like this:

Parent.java:

@Entity(name ="Parent")
         


        
5条回答
  •  一个人的身影
    2020-12-07 19:22

    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.

提交回复
热议问题