File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes hibernate one-to-one (on foreign key) vs one-to-one (on primary 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 (on foreign key) vs one-to-one (on primary key)" Watch "hibernate one-to-one (on foreign key) vs one-to-one (on primary key)" New topic
Author

hibernate one-to-one (on foreign key) vs one-to-one (on primary key)

samantha clarkson
Ranch Hand

Joined: Sep 09, 2008
Posts: 56
Hello dears ,

i have questions about what is the benefits of using a one-to-one association on foreign key or a one-to-one association on primary key, I've read the documentation of hibernate available at : http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/associations.html#assoc-unidirectional-121 can someone tell me with details the plus of using the first or the second implementation?

another question, i'have a one-to-one unidirectional relationship between User and Perimeter User ----> Perimeter and i want to use the association based on the foreign key. could i reverse the direction into User <---- Perimeter so the table User stay intact? I think that is not reasonable (perimeter.getUser() !!! ) but it's technically possible?

farm rubbit hihihihihihi, be aware !!
Emanuel Kadziela
Ranch Hand

Joined: Mar 24, 2005
Posts: 186
First of all note that the foreign key mapping is really a many-to-one with an extra restriction to make the foreign key column unique. The primary key way shares the primary key between both objects. In my mind, the latter implies a much stronger and inflexible one-to-one relationship. Take the Person and Address situation the document uses as an example, and imagine the requirements of your application suddenly changed to need multiple Persons for any Address. If you shared the primary key, you have much more refactoring to do. But if you used the foreign key, all you do is relax the unique constraint.

For your second question, let me clarify, you have two tables (and java entities (classes)) User and Perimeter and they are currently NOT related in any way, correct? Now, you would like to associate them in a one-to-one, unidirectional way, but you do not want to change the User table in any way?Well, if you do not want to change the User table, then the User table (or entity in java) will have no direct knowledge of the Perimeter table or entity. The Perimeter table will have a column referencing the User (foreign key into the User table), and the Perimeter entity will have a field User. What naturally follows, then, is that the Perimeter entity (class) will have a getter - perimeter.getUser(), returning the referenced User object.
samantha clarkson
Ranch Hand

Joined: Sep 09, 2008
Posts: 56
Hello Emanuel, your answer is perfect i wanted just what you said and it works totally fine Thank you a lot

Emanuel Kadziela wrote:First of all note that the foreign key mapping is really a many-to-one with an extra restriction to make the foreign key column unique. The primary key way shares the primary key between both objects. In my mind, the latter implies a much stronger and inflexible one-to-one relationship. Take the Person and Address situation the document uses as an example, and imagine the requirements of your application suddenly changed to need multiple Persons for any Address. If you shared the primary key, you have much more refactoring to do. But if you used the foreign key, all you do is relax the unique constraint.

For your second question, let me clarify, you have two tables (and java entities (classes)) User and Perimeter and they are currently NOT related in any way, correct? Now, you would like to associate them in a one-to-one, unidirectional way, but you do not want to change the User table in any way?Well, if you do not want to change the User table, then the User table (or entity in java) will have no direct knowledge of the Perimeter table or entity. The Perimeter table will have a column referencing the User (foreign key into the User table), and the Perimeter entity will have a field User. What naturally follows, then, is that the Perimeter entity (class) will have a getter - perimeter.getUser(), returning the referenced User object.
Emanuel Kadziela
Ranch Hand

Joined: Mar 24, 2005
Posts: 186
My pleasure
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: hibernate one-to-one (on foreign key) vs one-to-one (on primary key)