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

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

    If I am getting you correctly, according to EntityManager, if you want it to manage the transaction's insert order your have to "tell him" that it should persist the children too. And you are not doing that, so "he" doesn't know what to persist, but your parent's child list is not empty so "he" takes it has correct but the stored value is null.

    So you should consider do something like:

    ... begin, etc
    em.persist(child)
    em.persist(parent)
    

    do what you want with the parent object here then commit and this should work for similar cases too.

提交回复
热议问题