-->
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.  [ 7 posts ] 
Author Message
 Post subject: one-to-many Mapping to non-primary-key column
PostPosted: Tue Mar 27, 2007 5:34 am 
Newbie

Joined: Tue Mar 27, 2007 5:14 am
Posts: 2
Hi all

NHibernate version: 1.2.0RC1

Database: MSSQL2000

I have searched for solutions to this, but haven't found any, even though it doesn't seem too exotic:

I'm having two tables, for example like:

Code:
Persons(Id,Name,Address,ZipCode,etc.)
ZipCodes(Id,ZipCode,Town,etc.)

ZipCodes is related one-to-many to Persons, on the column named ZipCode (in both tables), but both tables has a surrogate key "Id" that is the primary key.

How do I map this in NHibernate (I'm using NHibernate.Mapping.Attributes, but since I can insert raw xml, a .hbm mapping file example will also do)?

Please don't ask me to change the database-schema, since I can't (the above example isn't the actual db-schema, but an example that points out the problem).

I have tried mappings along the lines of:

Code:
[Bag(1)]
[Key(2, Column = "ZipCode")]      
[OneToMany(3, ClassType = typeof(Person))]
public virtual IList<Person> Persons { ...

But that will map Person.ZipCode to ZipCode.Id (the primary key of the parent), and I can't figure out how to specify the column in the parent table (ZipCodes).

Any help is highly appreciated.

Best regards

/Kasper
[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 04, 2007 6:57 pm 
Newbie

Joined: Thu May 31, 2007 6:52 pm
Posts: 6
Did you ever find a solution to this problem? I'm facing the same problem and cannot find a solution.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 05, 2007 4:46 pm 
Newbie

Joined: Thu May 31, 2007 6:52 pm
Posts: 6
So what I've found so far is that NHibernate does not support unidirectional one-to-many relationships on a non primary, foreign key. But supposedly it supports bi-directional ones, so theoretically you could setup your mapping like so, and it should work:

Person Class (many-to-one side):
Code:
      <many-to-one name="ZipCode" column="ZipCode" property-ref="ZipCode" class="My.Example.ZipCodes, My.Example" />


ZipCode Class (one-to-many side):
Code:
      <bag name="Persons" table="tblPersons" lazy="true" inverse="true" >
         <key column="ZipCode" />
         <one-to-many class="My.Example.Persons, My.Example" />
      </bag>


According to what I read it this topic: Unidirectional one to many association on a FK You should be able to just add the property-ref="" attribute to the many-to-one side, and it should automatically link up the one-to-many side for you. But I can't get it to work for me... but I could be doing something else wrong.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 05, 2007 5:50 pm 
Newbie

Joined: Thu May 31, 2007 6:52 pm
Posts: 6
Looks to me like the XML mapper example I gave should work, but there is a bug described here in NHibernate 1.2.

This pretty much makes NHibernate unusable for me, as I'm trying to build a Business Layer for a legacy system, that uses this technique pervasively through out the system.

Can anyone confirm if this assessment is correct?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 06, 2007 11:43 am 
Regular
Regular

Joined: Fri May 05, 2006 11:54 am
Posts: 51
Can we have some clarity on this problem.

I have seen it in the forum many times without a reasonable answer


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 07, 2007 2:54 am 
Newbie

Joined: Tue Mar 27, 2007 5:14 am
Posts: 2
Well, I didn't find any good answer either.

My workaround is that I simply doesn't map the one-to-many relation, and instead I implement a property IList<Whatever> (using C#, .NET and NHibernate) myself, and let the getter of that property query the database and return the list of related objects.

To avoid having every object now about the NHibernate session, I have a factory class knowing the session, exposing methods to get the relevant lists. Maybe not the most elegant solution, but it works, and have also proven useful in controlling when to load what (I'm not really satisfied with lazy-loading, but that is another story).

Hope this is clear, and helps someone.

Best regards

Kasper


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 08, 2007 12:20 pm 
Newbie

Joined: Thu May 31, 2007 6:52 pm
Posts: 6
I had to hack a solution as well, I'm lucky enough that I have the ability to add and change views in the system I'm working on. So I actually just changed the view for the Child table to include a sub select on the parent table grabbing it's Primary key and then I was able to do the join in NHibernate as though I really did have a Primary Key / Foreign key reference.

(BTW the reason it has to be a sub select and not a join is because SQL Server won't let you update / delete from a view that has more than one table specified in the from clause.)


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