-->
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.  [ 4 posts ] 
Author Message
 Post subject: many-to-one with compositeId but using only parts of the key
PostPosted: Thu Aug 21, 2008 9:08 am 
Newbie

Joined: Thu Aug 21, 2008 8:40 am
Posts: 4
Hi,

I'm using NHibernate 1.2.1 and I've got a question concerning compositeId and a many-to-one relation:

Object A has got a composite id (Id1 and Id2). Some other object are implementing a many-to-one relation and are accessing to this object through the composite id. Everything's fine.

Object A (excerpt)
Code:
   <composite-id>
      <key-property name="Id1" column="ID1" />
      <key-property name="Id2" column="ID2" />
    </composite-id>


Any other Object accesses to it by:
Code:
    <many-to-one name="myReference" class="ObjectA">
      <column name="ForeignID1" />
      <column name="ForeignID2" />
    </many-to-one>


But the evil Object B has also a many-to-one relation and want's to access to object A, but only by using the first part of the composite id (Id1), because the use of both keys would return a different set of data.

Code:
    <many-to-one name="myReference" class="ObjectA">
      <column name="ForeignID1" />
    </many-to-one>


I've got an error when I try create my nhibernate session saying "Foreign key in table B must have same number of columns
as referenced primary key in table A". I understand what this means cause I normally use both parts of the composite id. But is there a way only to use one part of the key and ignore the second?

As I'm dependend to an existing data model and as I'm not mighty to change this data model, I'm searching for a way to connect my objects through this many-to-one relation using only the first part of the composite-id.

Any ideas, hints or remarks?
Thanks, aggn


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 21, 2008 9:21 am 
Regular
Regular

Joined: Tue Jul 29, 2008 3:30 am
Posts: 74
NHibernate usually only allows foreign keys (many-to-one references) to primary keys. But if you know that some property other then the primary key is unique in the referenced entity, you can use the attribute "property-ref" on your many-to-one relation.

From the documentation:
Quote:
The property-ref attribute should only be used for mapping legacy data where a foreign key refers to a unique key of the associated table other than the primary key. This is an ugly relational model. For example, suppose the Product class had a unique serial number, that is not the primary key. (The unique attribute controls NHibernate's DDL generation with the SchemaExport tool.)

<property name="serialNumber" unique="true" type="string" column="SERIAL_NUMBER"/>

Then the mapping for OrderItem might use:

<many-to-one name="product" property-ref="serialNumber" column="PRODUCT_SERIAL_NUMBER"/>

This is certainly not encouraged, however.


But such a reference would only work if you have a unique constraint on the column TableA.ID1 in your database!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 22, 2008 8:17 am 
Newbie

Joined: Thu Aug 21, 2008 8:40 am
Posts: 4
Thanks for your reply! I'll check that and give you some feedback.

Meanwhile I'm wondering, if this will also work with more than one column. E.g. I've got a composite id out of four parts but only want to refer to the first two keys...

... I've got a really ugly datamodel here...


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 25, 2008 1:53 am 
Regular
Regular

Joined: Tue Jul 29, 2008 3:30 am
Posts: 74
aggn wrote:
Thanks for your reply! I'll check that and give you some feedback.

You're welcome.

Quote:
Meanwhile I'm wondering, if this will also work with more than one column. E.g. I've got a composite id out of four parts but only want to refer to the first two keys...

I couldn't find something about that in the documentation, so I'm afraid that won't work.

Quote:
... I've got a really ugly datamodel here...

Agreed ;-)


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