-->
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.  [ 1 post ] 
Author Message
 Post subject: Problem with many-to-one join mapped using non-primary key
PostPosted: Tue Nov 03, 2009 1:04 pm 
Newbie

Joined: Sat Oct 31, 2009 9:38 am
Posts: 5
Hi,

We are facing issue with "Many to one" association. here we need to map with non-primary key column on parent table (which could be the source of the problem).

Following are our two tables and their mapping files.

Parent table mapping:
================
<id name="Id" column="primkey">
<generator class="identity" />
</id>
<property name="Code" column="code" />
<property name="CodeType" column="codetype" />
<property name="CodeName" column="codename" />
<property name="RecDesc" column="recdesc" />

<set name="SalesrepCodes" lazy="true">
<key column="mktregion" foreign-key="mktregion" />
<one-to-many class="SalesRep" />
</set>

Note: In the able table primkey is the actual primary key. But internally (i.e problematically) we have to consider "CodeName" as primary key column. CodeName is of data type char(2). This codeName column is used as foreign key (not created physically) in child table below

Child table:
========

<id name="Id" column="primkey">
<generator class="identity" />
</id>
<!--<version name="EntityVersion" column="recver" />-->
<property name="SalesRep" column="salesrep" />
<property name="MarketingRegionCode" column="mktregion" />
<property name="SalesRepName" column="repname" />
<property name="Phone" column="phone" />

<many-to-one name="MarketingRegionObject" lazy="proxy"
class="MarketingRegion" column="mktregion" fetch="join"
property-ref="CodeName" not-null="true"
cascade="all" />

Note: In the above table mktregion is the column that contains parent table codename values.

So, basically we are trying map many to one join using non-primary key column. our database has developed many years ago and we have to follow the same structure unfortunately.

Now our problem is below.

1. We are able load salesrep object with no issues. The corresponding marketing region of sales rep object is loading perfectly fine. But when we are trying load marketing region object (parent table), it is not loading list of associated sales rep objects (child table rows). When we observe output window of VS2008, it is comparing with primkey value of parent table instead of non-primary key column that we have mapped. Following is the sql statement nhibernate is trying to execute. 67 is the primary key column value in parent table.

FROM salesrep salesrepco0_ WHERE salesrepco0_.mktregion=?;p0 = 67

The correct statement should be

FROM salesrep salesrepco0_ WHERE salesrepco0_.mktregion=?;p0 = 'NW' (instead of primary key value it should pass non-primary key value that we have mapped)

Please let me know, whether we can map non-primary key columns in many to one joins or not. If we can map, then please let me know if any thing is wrong in our mapping files or any alternatives to satisfy our requirement.

Your help regarding this will be much appreciated.

Regards
Kishore


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

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.