-->
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.  [ 5 posts ] 
Author Message
 Post subject: Mapping non-primary-key columns in one-to-many
PostPosted: Wed Jul 19, 2006 9:10 pm 
Newbie

Joined: Mon Apr 24, 2006 6:31 pm
Posts: 3
I have a scenario that doesn't seem to exotic, but I can't find the answer, so I'm hoping that someone can help. I have two entities that I want to map together in a one-to-many relationship.

My containing entity has an integer primary key, but it also contains three other columns that act as a foreign key to my second entity, which has a compsite key made up of those three columns:

Class A
<class name="Calls" table="Calls" >
<id name="CallID" column="CallID" type="Int32" unsaved-value="0">
<generator class="identity" />
</id>
<property name="Location" column="Location" type="String" length="25" />
<property name="CustID" column="CustID" type="Int32" length="4" />
<property name="Library" column="Library" type="String" length="50" />
...
</class>

Class B
<class name="Calls_l" table="Calls_l" >
<composite-id>
<key-property name="CustID" column="CustID" type="Int32" length="4" />
<key-property name="Location" column="Location" type="String" length="25" />
<key-property name="Library" column="Library" type="String" length="50" />
</composite-id>
...
</class>

All I want to do is to create a bag of Bs in A, but the mapping expects the foreign key in B to map to the primary key in A. For example, this:

<bag name="Locations" >
<key>
<column name="Library"/>
<column name="Location"/>
<column name="CustID"/>
</key>
<one-to-many class="Calls_l"/>
</bag>

yields the following exception:

"Foreign key in Calls_l must have the same number of columns as referenced primary key in table Calls."

The problem is that I don't WANT to reference the primary key in Calls.
Yes, this problem seems stupid to me too. Can someone point me in a direction??


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 19, 2006 9:47 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Code:
<class name="Calls" table="Calls" >
  <id name="CallID" column="CallID" type="Int32" unsaved-value="0">
    <generator class="identity" />
  </id>
  <properties name="Calls_l_ref">
    <property name="Location" column="Location" type="String" length="25" />
    <property name="CustID" column="CustID" type="Int32" length="4" />
    <property name="Library" column="Library" type="String" length="50" />
  </properties>
  ...
  <bag name="Locations" >
    <key property-ref="Calls_l_ref">
      <column name="Library"/>
      <column name="Location"/>
      <column name="CustID"/>
    </key>
    <one-to-many class="Calls_l"/>
  </bag>
</class>

Class B
<class name="Calls_l" table="Calls_l" >
  <composite-id>
    <key-property name="CustID" column="CustID" type="Int32" length="4" />
    <key-property name="Location" column="Location" type="String" length="25" />
    <key-property name="Library" column="Library" type="String" length="50" />
  </composite-id>
...
</class>

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 20, 2006 2:37 pm 
Newbie

Joined: Mon Apr 24, 2006 6:31 pm
Posts: 3
Thanks for the response. I had tried this before, and confirmed again that "properties" doesn't seem to be a valid element under the class namespace. It throws the following exception when the configuration loads the assembly:

"The element 'class' in namespace 'urn:nhibernate-mapping-2.0' has invalid child element 'properties' in namespace 'urn:nhibernate-mapping-2.0'. List of possible elements expected: 'urn:nhibernate-mapping-2.0:discriminator urn:nhibernate-mapping-2.0:version urn:nhibernate-mapping-2.0:timestamp urn:nhibernate-mapping-2.0:property urn:nhibernate-mapping-2.0:many-to-one urn:nhibernate-mapping-2.0:one-to-one urn:nhibernate-mapping-2.0:component urn:nhibernate-mapping-2.0:dynamic-component urn:nhibernate-mapping-2.0:any urn:nhibernate-mapping-2.0:map urn:nhibernate-mapping-2.0:set urn:nhibernate-mapping-2.0:list urn:nhibernate-mapping-2.0:bag urn:nhibernate-mapping-2.0:idbag urn:nhibernate-mapping-2.0:array urn:nhibernate-mapping-2.0:primitive-array urn:nhibernate-mapping-2.0:subclass urn:nhibernate-mapping-2.0:joined-subclass'

Is this a Java Hibernate-only tag? Do I need a special reference in my assembly? I am using NHibernate v. 1.20 Alpha.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 20, 2006 5:44 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Yes, properties isn't in the hibernate2 mapping DTD. Can you play with your schema? You could add a join table to get around this, then map this as a many-to-many (that happens to be a one-to-many, but H2 doesn't have unique="true" on many-to-many). The join table would have the CallID, Location, CustID and Library columns from the Calls table.

There are other things that you can do to get this working, but they all involve tweaking your schema or upgrading to java. Which do you prefer? ;>

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 20, 2006 6:45 pm 
Newbie

Joined: Mon Apr 24, 2006 6:31 pm
Posts: 3
A Faustian choice!! Unfortunately, doing either would be very difficult at this point. It looks like I may have to punt: as a workaround, I was using HQL to get enumerable sets of the classes, and then "assembling" them in the DAO before returning the entity. This seems to defeat the composition that NHibernate affords - does anyone have any better ideas??


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