This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Object Relational Mapping and the fly likes Confusion over the Hibernate Bi-Direction OneToOne Association Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Confusion over the Hibernate Bi-Direction OneToOne Association" Watch "Confusion over the Hibernate Bi-Direction OneToOne Association" New topic
Author

Confusion over the Hibernate Bi-Direction OneToOne Association

Alan Clark
Greenhorn

Joined: Jan 17, 2010
Posts: 4
Hi,

I've just been reading Cameron McKenzie's Hibernate Made Easy (a great read, and hugely more entertaining than the very dry 'Hibernate in Action' book) but have hit a point of confusion that I'm hoping someone here could assist me with.

When Cameron describes the uni-directional 1-to-1 mapping, I'm completely 'with' everything that is going on...
The Exam class has-a ExamDetail class, and when mapped to the Exam table, I can see a new column called detail_id that will be used as the foreign key.
All very understandable and well explained.

However, when we move on to the bi-directional 1-to-1 example, although I understand that ExamDetail now needs an Exam property (with associated getter and setter methods), I do not understand why the Exam_Detail table does not acquire a new exam_id column to point back to the owning Exam record.

I think I must be missing something fundamental to my Hibernate understanding here...

My reasoning for wanting a bi-direction association would be that I might want to pull an Exam from the DB (using session.get() on the Exam class) and have that Exam object correctly populated with an ExamDetail object, but that I might ALSO want to pull an ExamDetail from the DB and have it correctly populated with its associated Exam object.

If the Exam table ONLY has a foreign key (to Exam_Detail) then I can see the former query working perfectly efficiently (given that both tables had indexed primary keys), but the only way to find out which Exam record points to a particular Exam_Detail record would be by performing a whole table search of the Exam table. Hence the look-up is not symetrically bi-directional.

What have I got wrong here?

Many thanks,
Clarky
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

Howdy Greenhorn! Great, detailed post for a new cow-poke. That's great to see.

For those unfamiliar with this example from my little book, here's the Java class diagram:

And here's the ERD diagram:


I think the real key is that the id (primary key) in the ExamDetail is always mapped back to the associated detail_id in the Exam class. That way, given the id of the ExamDetail, you can always find the associate Exam through the foreign key, detail_id, in the Exam, and vice-versa.

The key to this whole thing is the @OneToOne annotation:




At the database level, this is all enforced by a database constraint. Here's the DDL that Hibernate generates when you ask for a schema_export of this example. Notice the constraint in the Exam table:



Does that make any sense?

Great to see you around these parts, Greenhorn!

-Cameron McKenzie

Alan Clark
Greenhorn

Joined: Jan 17, 2010
Posts: 4
Cameron,
All clear now

Many thanks for taking the time to get me to really see what is going on under the covers, and so quickly too!

BTW, is anyone pestering you to produce a "Spring MVC Made Easy with Annotations" book?
If not, then consider me the first!

All the best,
Clarky
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

Consider yourself the millionth...

I'm finishing up a JSF1.2 and JSF 2.0 Made Easy resource. When that's done, I'm looking at a VERY SMALL book on Spring.

I love Spring, and it's so elegant and simple when used properly. I'm flabberghasted at how it has somehow become so complicated.

In time...in time...

*Stop hijacking your own threads, Greenhorn!*

-Cameron McKenzie
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Confusion over the Hibernate Bi-Direction OneToOne Association
 
Similar Threads
How to Set Primary / Foreign Keys or Relationships with Hibernate 3 / JPA Tables?
Hibernate, User and Address with association table between
Hibernate POJOs -- association or no-association ?
uni-directional association (or Directed Association ) vs dependency
updating association table