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: Foreign key in table xxx must have same number of columns
PostPosted: Tue Jan 22, 2008 2:12 am 
Newbie

Joined: Thu May 04, 2006 1:07 pm
Posts: 2
Location: Ohio
Hello all. I seem to be having an issue with my mapping file and I am baffled. From reviewing the documentation and notes posted by others, my mapping file seems to be correct, however on calling the following code:
Code:
            LOGGER.Debug("Initializing the NHibernateHelper class");
            config = new Configuration();
            config.Configure();
            sessionFactory = config.BuildSessionFactory();


I find that the system dies on the fourth line with the error about the Foreign key needing the same number of columns as the primary key.

Basically I am trying to establish a many-to-one relationship from an Order class to an Address class. The Address class is defined (legacy data, I have no control and cannot change the structure) as using a composite identifier that is the CUSTOMER_ID + a sequence number that is unique per customer. So I have defined the composite-id in the mapping file for the Address class.

In the Order table (my table in this case), I have added two fields to reference the billing address (I've done the same for the shipping address but that can be ignored for now so as not to complicate the issue), CUSTOMER_ID and BILL_TO_ADDR_ID. ORDERS.CUSTOMER_ID + ORDERS.BILL_TO_ADDR_ID should be an FK to ADDRESS.CUSTOMER_ID + ADDRESS.ADDR_NO.

Can anyone spot what is wrong or point me to another post that may explain this issue? I've read the docs (chapters 5 and 6) regarding this issue and did not find the answer within.

Could this be specific to the fact that the composite-id also contains a key-many-to-one directive?

Thanks,
Justin

Hibernate Version:
NHibernate 1.2.0.4000

Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="Phoenix.Orders" assembly="Online Order Library" default-lazy="true">
  <class name="Phoenix.Orders.Order, Online Order Library" table="ORDERS">
    <!-- -->
    <id name="Id">
      <column name="ORDER_ID" not-null="true"/>
      <generator class="sequence">
        <param name="sequence">ORDERS_SEQ</param>
      </generator>
    </id>

    <!-- A cat has to have a name, but it shouldn' be too long. -->
    <property name="OrderNumber" column="ORDER_NO"/>
    <list name="OrderLines">
      <key column="ORDER_ID" />
      <index column="LINE_NO" />
      <one-to-many class="Phoenix.Orders.OrderLine" />
    </list>
    <many-to-one name="Customer" column="CUSTOMER_ID" class="Phoenix.Customers.Customer, Online Order Library"  cascade="none" update="false" insert="false" />
    <property name="OrderDate" column="ORDER_DATE"/>
    <many-to-one name="BillTo" class="Phoenix.Customers.Address, Online Order Library" cascade="none" update="false" insert="false">
      <column name="CUSTOMER_ID" />
      <column name="BILL_TO_ADDR_ID" />
    </many-to-one>
    <!-- many-to-one name="ShipTo" class="Phoenix.Customers.Address, Online Order Library"  cascade="none" update="false" insert="false">
      <column name="CUSTOMER_ID" />
      <column name="SHIP_TO_ADDR_ID" />
    </many-to-one -->
    <property name="DesiredShipDate" column="DESIRED_SHIP_DATE"/>
    <property name="Status" column="STATUS"/>
    <property name="SpecialNotes" column="NOTES"/>
    <property name="OrderType" column="ORDER_TYPE"/>
    <property name="CreatedBy" column="CREATED_BY"/>
    <property name="Created" column="CREATED"/>
    <property name="UpdatedBy" column="UPDATED_BY"/>
    <property name="Updated" column="UPDATED"/>
    <many-to-one name="DisclaimerAcceptance" class="Phoenix.Orders.DisclaimerAcceptance" column="ORDER_ID" unique="true" />
  </class>


<?xml version="1.0" encoding="utf-8" ?>
<!--
  Mapping file for the CUST_ADDRESS_VW view used to display the available addresses for a customer.
  -->
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="Phoenix.Customers" assembly="Online Order Library">
  <class name="Phoenix.Customers.Address, Online Order Library" table="CUST_ADDRESS">
    <!--
      There is no natural ID for this table so the CUSTOMER_ID and ADDR_NO are those fields defined to make
      up the unique ID for each row.
      -->
    <composite-id name="AddressKey" class="AddressKey">
      <key-many-to-one name="Customer" class="Phoenix.Customers.Customer" column="CUSTOMER_ID"/>
      <key-property name="AddressNumber" type="long" column="ADDR_NO"/>
    </composite-id>

    <property name="Line1">
      <column name="ADDR_1" length="50" not-null="false" />
    </property>
    <property name="Line2">
      <column name="ADDR_2" length="50" not-null="false" />
    </property>
    <property name="Line3">
      <column name="ADDR_3" length="50" not-null="false" />
    </property>
    <property name="City">
      <column name="CITY" length="30" not-null="false" />
    </property>
    <property name="State">
      <column name="STATE" length="10" not-null="false" />
    </property>
    <property name="Country">
      <column name="COUNTRY" length="50" not-null="false" />
    </property>
    <property name="PostalCode">
      <column name="ZIPCODE" length="10" not-null="true" />
    </property>
  </class>

</hibernate-mapping>
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():
N/A

Full stack trace of any exception that occurs:
Code:

[MappingException: Foreign key in table ORDERS must have same number of columns as referenced primary key in table CUST_ADDRESS]
   NHibernate.Mapping.ForeignKey.set_ReferencedTable(Table value) +205
   NHibernate.Cfg.Configuration.SecondPassCompileForeignKeys(Table table, ISet done) +652
   NHibernate.Cfg.Configuration.SecondPassCompile() +787
   NHibernate.Cfg.Configuration.BuildSessionFactory() +32
   Phoenix.Utility.NHibernateHelper..cctor() in C:\Documents and Settings\justin.spies\My Documents\Visual Studio 2005\Projects\Online Ordering\OnlineOrderLibrary\Utility\NHibernateHelper.cs:35

[TypeInitializationException: The type initializer for 'Phoenix.Utility.NHibernateHelper' threw an exception.]
   Phoenix.Utility.NHibernateHelper.GetCurrentSession() in C:\Documents and Settings\justin.spies\My Documents\Visual Studio 2005\Projects\Online Ordering\OnlineOrderLibrary\Utility\NHibernateHelper.cs:65
   Phoenix.Security.SecurityServiceImpl.login(String username, String password) in C:\Documents and Settings\justin.spies\My Documents\Visual Studio 2005\Projects\Online Ordering\OnlineOrderLibrary\Security\SecurityServiceImpl.cs:21
   Login.LoginButton_Click(Object sender, EventArgs e) in d:\Development\Projects\Phoenix Specialty\Online Ordering\security\Login.aspx.cs:37
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +96
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +116
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +31
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +32
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +72
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3838


Name and version of the database you are using:
Oracle 9i (9.2.0.8.0)

The generated SQL (show_sql=true):
N/A

Debug level Hibernate log excerpt:
Code:
25578 DEBUG 13 NHibernate.Cfg.CollectionSecondPass - Mapped collection key: USER_ID, one-to-many: Customer
25578 INFO 13 NHibernate.Cfg.Configuration - processing one-to-one association property references
25578 INFO 13 NHibernate.Cfg.Configuration - processing foreign key constraints
25593 DEBUG 13 NHibernate.Cfg.Configuration - resolving reference to class: User
25593 DEBUG 13 NHibernate.Cfg.Configuration - resolving reference to class: Order
25593 DEBUG 13 NHibernate.Cfg.Configuration - resolving reference to class: Product
25593 DEBUG 13 NHibernate.Cfg.Configuration - resolving reference to class: Customer
25593 DEBUG 13 NHibernate.Cfg.Configuration - resolving reference to class: User
25593 DEBUG 13 NHibernate.Cfg.Configuration - resolving reference to class: Customer
25593 DEBUG 13 NHibernate.Cfg.Configuration - resolving reference to class: Order
25593 DEBUG 13 NHibernate.Cfg.Configuration - resolving reference to class: Disclaimer
25593 DEBUG 13 NHibernate.Cfg.Configuration - resolving reference to class: Customer
25593 DEBUG 13 NHibernate.Cfg.Configuration - resolving reference to class: Customer
25593 DEBUG 13 NHibernate.Cfg.Configuration - resolving reference to class: Customer
25593 DEBUG 13 NHibernate.Cfg.Configuration - resolving reference to class: Address
25609 ERROR 13 NHibernate.Cfg.Configuration - NHibernate.MappingException: Foreign key in table ORDERS must have same number of columns as referenced primary key in table CUST_ADDRESS
   at NHibernate.Mapping.ForeignKey.set_ReferencedTable(Table value)
   at NHibernate.Cfg.Configuration.SecondPassCompileForeignKeys(Table table, ISet done)
[/code]


Top
 Profile  
 
 Post subject: Foreign key in table xxx must have same number of column
PostPosted: Thu Jan 24, 2008 10:01 am 
Newbie

Joined: Thu May 04, 2006 1:07 pm
Posts: 2
Location: Ohio
I managed to resolve this issue and it appears to have had nothing to do with NHibernate.

After some successful testing involving a parent, compositekey and child dummy class, I went back to my original code and copied the code with new names. This same code, just copied and renamed, worked fine with the many-to-one scenario.

I deleted my original classes and associated HBM files, leaving the newly working copies. I then renamed my newly working copies (from CompositeParent, CompositeKey and CompositeChild) to the original names (Address, AddressKey and Order) as well as the HBM files. Amazingly the system stopped reporting errors and started working. So it appears that something got messed up in the CLR / Compiler / something.


Top
 Profile  
 
 Post subject: Yes It´s VS IDE
PostPosted: Wed Jun 04, 2008 9:01 pm 
Newbie

Joined: Fri May 02, 2008 1:27 pm
Posts: 4
Location: Mexico
I´m very tired but finally i resolved this problem, the Mappings are fine, I did this:

* Kill all the aspnet_wp.exe (WinX) or w3wp.exe (WinServer 2003)
* Rebuild your Solution and then Run your app.

I hope woks this solution for you.

Bye


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.