-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: One-to-one mapping with a specified column
PostPosted: Thu Jul 19, 2007 10:19 am 
Beginner
Beginner

Joined: Thu Jan 22, 2004 2:42 pm
Posts: 24
Hey, I'm trying to map a one-to-one relationship from a table I'm creating to a table that currently exists; the default behavior for a one-to-one relationship is just to have the two entities have the same id value; this is not an option for me. I want to create a one-to-one relationship in which hibernate maps the relation with an additional column in the table.

For example:
I want a one-to-one relation of students to lockers;

so I have student(id, name) and locker (id, number). The default behavior is for the student and locker to have the same id value; what I need to do is: student(id, name, lockerid) and locker(id, number) where lockerid is the id of the locker entry. I can't seem to make the one-to-one mapping behave like this.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 24, 2007 6:56 am 
Newbie

Joined: Sat Oct 28, 2006 6:16 am
Posts: 17
There is a bit at the end of section 5.1.11 in the hibernate documentation about this (http://www.hibernate.org/hib_docs/v3/reference/en/html_single/#mapping-declaration-onetoone).

It is a little bit unintuitive but you need to use a many-to-one association with the unique setting set to true instead of one-to-one.

So for your particular case I would expect the mapping for the Student class to contain something like:

Code:
<many-to-one name="locker" class="Locker" column="lockerid" unique="true"/>


If the association is bi-directional then I would also expect the mapping for the Locker class to contain something like:

Code:
<one-to-one name"student" class="Student" property-ref="locker"/>


Cheers


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.