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

后端 未结 5 1800
太阳男子
太阳男子 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.

    0 讨论(0)
  • 2020-12-07 19:17

    We ran into a problem while persisting a simple object graph like the one shown above. Running in H2 everything would work, but when we ran against MySQL the "paren_id" in the child table (defined in the @JoinColumn annotation) wasn't getting populated with the generated id of the parent - even though it was set as a non-null column with a foreign key constraint in the DB.

    We'd get an exception like this:

    org.hibernate.exception.GenericJDBCException: Field 'paren_id' doesn't have a default value
    

    For anyone else who might run into this, what we eventually found was that we had to another attribute to the @JoinColumn to get it to work:

    @JoinColumn(name="paren_id", nullable=false)
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-07 19:27

    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.

    0 讨论(0)
提交回复
热议问题