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 Hibernate one-to-one mapping with foreign key Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate one-to-one mapping with foreign key" Watch "Hibernate one-to-one mapping with foreign key" New topic

Hibernate one-to-one mapping with foreign key

sean van buggenum

Joined: May 16, 2008
Posts: 7
Hi all,

I was starting to learn hibernate, and found it a bit difficult to start up, and thought it would be helpful for me (and others after me) if I were to create a bit of a tutorial.
To write the tutorial, I will have to solve many problems. And once finished, i've come through a journey of learning, and I have written it all down, and will hopefully help others come through the same journey, but faster.

I am now stuck, however, with what should be a simple association. If anyone can help, i'd be very grateful;

I have the following situation:

1. A User has many contacts;
2. A Contact has many contact details

This part was easy. And I was able to get it working without trouble. I could demonstrate the one-to-many relationship between the user and his contacts, and the contact and his contact details.
This was all represented in memory by a few classes;
class User,
class Contact,
class ContactDetail

class User holds a Set of Contact types;

and class Contact holds a Set of ContactDetails types;

However, when I wanted to demonstrate how to additionally say that our User also has one contact detail himself (A main contact detail), by saying

I have not been able to work out how to represent this in the hibernate xml configuration.

The end result that I want is that my User table also has a foreign key id in its table which points to the correct primary key in my ContactDetails table.
At the moment, all i've achieved is the primary key of my User table incorrectly being joined (apparently) to the primary key of the ContactDetails table.

Here are my configurations so far;

Obviously I need something in my "<class name="" " element ....
I thought a 'one-to-one' element, but I can't for the life of me see how I should configure it.

Anyone? thanks in advance.

p.s. I tried to attach the project here (only 9kb) but coderanch doesn't accept .zip files .... damn)>
sean van buggenum

Joined: May 16, 2008
Posts: 7
I should just now mentioned, after continuing to play, I seem to have got something that works for me. However, my question still stands as I don't fully understand the solution i've found.

My new mapping for the User class, to allow the one-to-one relationship with User and a single ContactDetail is as follows (note the many-to-one element)

Additionally, I read from the hibernate website:

regarding the unique attribute

unique (optional): enables the DDL generation of a unique constraint for the foreign-key column. By allowing this to be the target of a property-ref, you can make the association multiplicity one-to-one.

Problem is, I do not fully understand the text "By allowing this to be the target of a property-ref".

Now, I know I have used a many-to-one element, ....... and i'm kind of satisfied that in looking at the actual tables that are created, I see exactly what I hoped to see: a contactDetails column in the User table, which associates with the DetailId of the ContactDetails table.

What I would like to know is;

1. Have I done it the best way
2. Can the way I've done it be improved (either practically, or theoretically)
3. If there is another way to do it, how could I have done it?
4. What does all that stuff about the property-ref mean anyway, and in what situations would I use it, if not here?
5. Related to (4, 2), have I done enough by specifying unique="true" in the many-to-one relationship? That is, I do not want to allow other users referring to the same ContactDetail. Have I solved this with the unique contstraint here?

thanks again!
p.s. I'll be sure to post the final tutorial when it is finished.... if any reader is interested.

sean van buggenum

Joined: May 16, 2008
Posts: 7
I still haven't really found my confirmation. But I am fairly sure it is the 'unique=true' setting that provides what I need.

Incidentally, while not finished, I would like to share what I have written so far.
Hopefully someone finds it interesting one day;

I agree. Here's the link:
subject: Hibernate one-to-one mapping with foreign key
jQuery in Action, 3rd edition