-->
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.  [ 3 posts ] 
Author Message
 Post subject: Can someone help with a Many-to-one with a composite-id?
PostPosted: Thu Jun 03, 2010 2:20 pm 
Newbie

Joined: Mon Feb 09, 2009 11:52 am
Posts: 11
So here is the deal I have 2 tables that look like this...

User:
ID
Name

Address:
ID(User)
Type
Line1
Line2
....

Where the PK for Address is a combination of ID and Type.

Here is my mapping file...
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
   auto-import="true">
  <class name="AutomatedGrantSuggestions.DataAccess.Organization, AutomatedGrantSuggestions.DataAccess" lazy="false" table="ct_org_master">
    <id name="CtId" column="ct_id">
      <generator class="native" />
    </id>
    <property name="Name" column="ct_org_name1"/>
    <property name="Name2" column="ct_org_name2"/>
    <many-to-one name="Address" class="AutomatedGrantSuggestions.DataAccess.Address, AutomatedGrantSuggestions.DataAccess"/>
  </class>
  <class name="AutomatedGrantSuggestions.DataAccess.Address, AutomatedGrantSuggestions.DataAccess" lazy="false" table="ct_org_address">
    <composite-id>
      <key-property name="CtId" column="ct_id" />
      <key-property name="Type" column="ct_org_addr_type"/>
    </composite-id>
    <property name="CtId" column="ct_id"/>
    <property name="Line1" column="ct_org_addr1"/>
    <property name="Line2" column="ct_org_addr2"/>
  </class>
</hibernate-mapping>


But when I run my tests I get the following error...
Quote:
Test 'AutomatedGrantSuggestions.DataAccess.Tests.OrganizationIntegrationTest.TestRetrieveHibernateFromIDWithAddress' failed:
Test method AutomatedGrantSuggestions.DataAccess.Tests.OrganizationIntegrationTest.TestRetrieveHibernateFromIDWithAddress threw exception: NHibernate.FKUnmatchingColumnsException: Foreign key (FK4961A1696CC0F1CB:ct_org_master [Address])) must have same number of columns as the referenced primary key (ct_org_address [ct_id, ct_org_addr_type]).
at NHibernate.Cfg.Configuration.LogAndThrow(Exception exception)
at NHibernate.Cfg.Configuration.SecondPassCompileForeignKeys(Table table, ISet done)
at NHibernate.Cfg.Configuration.SecondPassCompile()
at NHibernate.Cfg.Configuration.BuildSessionFactory()
HibernateUtils.cs(47,0): at AutomatedGrantSuggestions.DataAccess.HibernateUtils.OpenSession()
HibernateUtils.cs(25,0): at AutomatedGrantSuggestions.DataAccess.HibernateUtils.getOrg(Int32 orgId)
OrganizationIntegrationTest.cs(167,0): at AutomatedGrantSuggestions.DataAccess.Tests.OrganizationIntegrationTest.TestRetrieveHibernateFromIDWithAddress()


Any Ideas?


Top
 Profile  
 
 Post subject: Re: Can someone help with a Many-to-one with a composite-id?
PostPosted: Thu Jun 03, 2010 2:23 pm 
Newbie

Joined: Mon Feb 09, 2009 11:52 am
Posts: 11
Haha oops saw someone else's right underneath teach me not to search first sorry guys...


Top
 Profile  
 
 Post subject: Re: Can someone help with a Many-to-one with a composite-id?
PostPosted: Thu Jun 03, 2010 3:15 pm 
Newbie

Joined: Mon Feb 09, 2009 11:52 am
Posts: 11
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
   auto-import="true">
  <class name="AutomatedGrantSuggestions.DataAccess.Organization, AutomatedGrantSuggestions.DataAccess" lazy="false" table="ct_org_master">
    <id name="CtId" column="ct_id">
      <generator class="native" />
    </id>
    <property name="Name" column="ct_org_name1"/>
    <property name="Name2" column="ct_org_name2"/>
    <bag name="AddressList" inverse="true" generic="true" lazy="true">
      <key>
        <column name="CtId" />
      </key>
      <one-to-many class="AutomatedGrantSuggestions.DataAccess.Address, AutomatedGrantSuggestions.DataAccess" not-found="ignore"/>
    </bag>
  </class>
  <class name="AutomatedGrantSuggestions.DataAccess.Address, AutomatedGrantSuggestions.DataAccess" lazy="false" table="ct_org_address">
    <composite-id>
      <key-property name="CtId" column="ct_id" />
      <key-property name="Type" column="ct_org_addr_type"/>
    </composite-id>
    <property name="Line1" column="ct_org_addr1"/>
    <property name="Line2" column="ct_org_addr2"/>
    <!--<many-to-one name="CtId" not-null="false"/>-->
  </class>
</hibernate-mapping>


This seems to work in case anyone else gets stuck...


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