Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Newbie Hibernate: @OneToMany as owning side

 
Denis Wen
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
His,

just starting my small personal project, which is another SAT words learning app. I have a data structure containing Word that has one or more possible Meaning(s). What would be a proper way of annotating the classes? So far i have



But Hibernate doc says "To map a bidirectional one to many, with the one-to-many side as the owning side, you have to remove the mappedBy element and set the many to one @JoinColumn as insertable and updatable to false. This solution is not optimized and will produce some additional UPDATE statements."

I don't really understand what "owning side of the relationship" implies, but suppose that my Word should be the owning side in the case. Could maybe someone clarify this point and suggest a better mapping that does not "produce additional UPDATE statements".
Also what effect do the the directives "insertable = false, updatable = false" have?
 
Bill Gorder
Bartender
Posts: 1682
7
Android IntelliJ IDE Linux Mac OS X Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The owning side of the relationship is the one that manges the relationship. I would put a mappedBy="word" on the OneToMany. This specifies that in this bi-directional relationship the Meaning class is the owning side. In a scenario such as this the @ManyToOne should always be the owning side and the @OneToMany is the non-owning or inverse side. If you do this you can drop the insertable and updatable attributes. These would no longer be needed, they specify whether it is 'Meaning''s responsibility to update or insert to this join column the reason they were required in your original mapping is because your @OneToMany was not inverse. This is suboptimal and would result in extra queries.

Try this:



Note that the mappedBy automatically makes the relationship the inverse or non-owning side.

There was some more discussion on this in this thread.
http://www.coderanch.com/t/593523/ORM/databases/JPA-persist-one-many-relationship
 
Denis Wen
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply. I will read up on the link you've sent me.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic