• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Confusion over the Hibernate Bi-Direction OneToOne Association

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author and cow tipper
Posts: 5009
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5009
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
reply
    Bookmark Topic Watch Topic
  • New Topic