This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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.
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!
Joined: Jan 17, 2010
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,
Cameron Wallace McKenzie
author and cow tipper