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

后端 未结 5 1809
太阳男子
太阳男子 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:14

    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.

提交回复
热议问题