Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

14
  • 27
    The best answer i find that explains the Doctrine 'mappedBy' +'inversedBy'. Commented Jul 7, 2014 at 6:18
  • 3
    I don't know if things have changed, but on Hibernate 5.0.9.Final if I " only call person.getDocuments().add(document), " hibernate updates the foreign key in ID_DOCUMENTS. Commented Apr 14, 2016 at 13:12
  • 3
    @Karl Nicholas , hi, agree with you we have cascade attribute on @OneToMany annotation that can be set to PERSIST in this case hibernate will save all linked entities to the DB . Can anybody please clarify this - why author says that hibernate will not track changes on non owning side - but in fact hibernated performs tracking ? Commented Oct 23, 2017 at 11:27
  • 7
    cascade tells the provider to save the children entities even if the parent entity doesn't own them, so it effectively modifies the rule. If you had (or have) mappedBy=child.field and did NOT have cascade then the children of the list wouldn't be saved. Also, if you didn't have mappedBy AND didn't have cascade then the Parent owns the relationship and if you put NEW children in the list and then save the Parent it will throw an exception because the new children ID's aren't available to be saved in the join table. Hope that clarifies things ... :) Commented Oct 23, 2017 at 15:03
  • 4
    To further clarify CASCADE, it is about parent/child relationship rather than owner/owned. So it is unrelated to ownership. Ownership determines how to define/store the relationship in the database, in other words, in which table and column(s). Parent/child on the other hand, determines how an action(i.e. persist, remove) should propagate to the related entities. So for instance, for the Order - LineItem bi-directional relationship with CASCADE=REMOVE on the Order.LineItems property, when you remove the Order, LineItem (which is the owner) will be removed due to parent -> child cascade. Commented Jan 26, 2019 at 2:57