File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Object Relational Mapping and the fly likes Newbie Hibernate: @OneToMany as owning side Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Newbie Hibernate: @OneToMany as owning side" Watch "Newbie Hibernate: @OneToMany as owning side" New topic

Newbie Hibernate: @OneToMany as owning side

Denis Wen
Ranch Hand

Joined: Nov 11, 2008
Posts: 33

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

Joined: Mar 07, 2010
Posts: 1682

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.

[How To Ask Questions][Read before you PM me]
Denis Wen
Ranch Hand

Joined: Nov 11, 2008
Posts: 33
Thanks for the reply. I will read up on the link you've sent me.
I agree. Here's the link:
subject: Newbie Hibernate: @OneToMany as owning side
It's not a secret anymore!