-->
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.  [ 6 posts ] 
Author Message
 Post subject: One-to-one mappings
PostPosted: Mon Feb 27, 2006 7:50 am 
Newbie

Joined: Mon Feb 27, 2006 7:23 am
Posts: 4
I have an issue with one-to-one mappings, and I am wondering whether the issue I encounter is a NHibernate (1.0.2) bug, or a problem with my understanding of one-to-one's.

The situation I have is quite simple. A person has one account and an account is for one person only.

Database:
Person (Id, Account)
Account(Id)

Mapping file for account:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class lazy="true" name="Account" table="Account">
<id name="Id" type="Guid" unsaved-value="00000000-0000-0000-0000-000000000000">
<column name="Id" sql-type="uniqueidentifier" not-null="true" unique="true" index="PK_Account"/>
<generator class="guid.comb" />
</id>
<one-to-one name="Person" cascade="all" constrained="true" property-ref="Account" class="ResPerson"/>
</class>
</hibernate-mapping>


Mapping file for person:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class lazy="true" name="ResPerson" table="ResPerson">
<id name="Id" type="Guid" unsaved-value="00000000-0000-0000-0000-000000000000">
<column name="Id" sql-type="uniqueidentifier" not-null="true" unique="true" index="PK_ResPerson"/>
<generator class="guid.comb" />
</id>
<many-to-one name="Account" class="Account" cascade="all">
<column name="AccountRef" sql-type="uniqueidentifier" not-null="false" index="IX_Account"/>
</many-to-one>
</class>
</hibernate-mapping>


The issue is, that when I create an account and set the person for an account (Account account = new Account(); account.Person = aPerson) the person table is not updated with the key to Person.

When I create a new account and do it the other way around (Account account = new Account(); aPerson.Account = account) the foreign key is set correctly.

Does anybody have any tips?

Thanks, Bastiaan de Rijber


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 28, 2006 3:56 pm 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
I think you should use <generator class="foreign"> in one of those classes.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 01, 2006 2:38 am 
Newbie

Joined: Mon Feb 27, 2006 7:23 am
Posts: 4
sergey wrote:
I think you should use <generator class="foreign"> in one of those classes.


Thanks for your tip Sergey, but from what I gather the foreign generator is used primarily for one-to-one relationsships where you'd use the primary key to to match the two tables - e.g. you use Account.Id to match Person.Id - in this case I have a foreign key on Person. Now, what I might have been missing is a unique constraint on the many-to-one, as mentioned in the documentation, but I fail to see how this would impact hibernate's persistance and/or property set logic.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 01, 2006 5:45 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
Ah ok, it was me getting stupid again, sorry... The person table is not updated in the first case because you are not changing the person, only the account.

Essentially, the information contained in Person object gets stored into ResPerson table, and the information in the Account objects gets stored into Account table, according to the mapping files. NHibernate doesn't try to be smart here and leaves it to you to maintain both sides of the relationship.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 01, 2006 6:27 am 
Newbie

Joined: Mon Feb 27, 2006 7:23 am
Posts: 4
Ahh, right... so it's basically the same situation as with the one-to-many and many-to-one mappings.

It's not quite what you'd expect for one-to-ones, but I guess if you are aware of what's actually happening there it's understandable.

So, basically, the suggested approach for this kind of situation is to change the domain model?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 01, 2006 11:15 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
Just make sure that when you are setting Person.Account you also set Account.Person, and vice versa. This should be enough.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.