-->
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.  [ 2 posts ] 
Author Message
 Post subject: Problem with composite key in one-to-many relationship
PostPosted: Fri Oct 07, 2005 4:51 am 
Newbie

Joined: Tue Oct 04, 2005 6:20 am
Posts: 9
I've looked at hibernate forum and docs, at nhibernate forum and docs and I can't find a solution to this problem.

I have a mapping
Code:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
   <class name="PerpetualInventory.Part, PerpetualInventory" table="PMA_TBL">   
   
      <!-- The table uses a the part only and part rev fields as a unique identifier -->
      <composite-id>
         <key-property name="PartOnly" column="PMA_PART_ONLY" access="field.pascalcase-m-underscore"/>
         <key-property name="PartRev" column="PMA_PART_REV" access="field.pascalcase-m-underscore"/>
      </composite-id>
            
      <!-- Map the table fields -->
      <property name="Desc" column="PMA_DESC" access="field.pascalcase-m-underscore"/>
      <property name="Desc_1" column="PMA_DESC_1" access="field.pascalcase-m-underscore"/>
      <property name="ProcurementCode" column="PMA_PROC_CODE" access="field.pascalcase-m-underscore"/>
      <property name="ABC" column="PMA_ABC_CODE" access="field.pascalcase-m-underscore"/>
      <property name="UOM" column="PMA_UOM" access="field.pascalcase-m-underscore"/>
      <property name="LastCheckDate" column="PMA_LAST_CHECK_DATE" access="field.pascalcase-m-underscore" not-null="false"/>
   
      <!--There is a one-to-many relationship between the Part and MRPOutput based upon the PartOnly
         and the PartRev-->      
      <bag name="MRPOutputs" inverse="true" access="field.pascalcase-m-underscore">
         <key>
            <column name="PMA_PART_ONLY" />
            <column name="PMA_PART_REV" />
         </key>
         <one-to-many class="PerpetualInventory.MRPOutput, PerpetualInventory"/>
      </bag>
         
      
      <!--There is a one-to-many relationship between the Part and Lot based upon the PartOnly
         and the PartRev-->
      <bag name="Lots" inverse="true" lazy="true" access="field.pascalcase-m-underscore">
         <key>
            <column name="PMA_PART_ONLY"/>
            <column name="PMA_PART_REV"/>
         </key>
         <one-to-many class="PerpetualInventory.Lot, PerpetualInventory"/>
      </bag>
      
   </class>

</hibernate-mapping>



Code:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
   <class name="PerpetualInventory.MRPOutput, PerpetualInventory" table="SCF_TBL">   
   
      <id name="ID" column="SCF_UNIQUE" type="Int32" unsaved-value="0" access="field.pascalcase-m-underscore">
         <generator class="identity"/>
      </id>
         
      <property name="OrderDate" column="SCF_ORDER_DATE" type="DateTime" access="field.pascalcase-m-underscore"/>
            
      <many-to-one name="Part" class="PerpetualInventory.Part, PerpetualInventory"
                              access="field.pascalcase-m-underscore"
                               not-null="false" cascade="none">
               <column name="SCF_PART_ONLY"  unique-key="PMA_PART_ONLY"/>
               <column name="SCF_PART_REV"   unique-key="PMA_PART_REV"/> 
      </many-to-one>
      
      
   </class>

</hibernate-mapping>



When I set the lazy attribute for the mapping for MRPOutputs to true it works fine, when I remove the lazy attribute (as shown above) I get a 'Probelm with find' error message.

The output shows:

Code:

4547 [3992] ERROR NHibernate.ADOException  - problem in find
Exception: System.Data.SqlClient.SqlException
Message: Invalid column name 'PMA_PART_ONLY'.
Source: .Net SqlClient Data Provider
   at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream)
   at System.Data.SqlClient.SqlCommand.ExecuteReader()
   at System.Data.SqlClient.SqlCommand.System.Data.IDbCommand.ExecuteReader()
   at NHibernate.Impl.BatcherImpl.ExecuteReader(IDbCommand cmd)
   at NHibernate.Loader.Loader.GetResultSet(IDbCommand st, RowSelection selection, ISessionImplementor session)
   at NHibernate.Loader.Loader.DoFind(ISessionImplementor session, QueryParameters parameters, Object optionalObject, Object optionalID, PersistentCollection optionalCollection, Object optionalCollectionOwner, Boolean returnProxies)
   at NHibernate.Loader.Loader.DoFindAndInitializeNonLazyCollections(ISessionImplementor session, QueryParameters parameters, Object optionalObject, Object optionalID, PersistentCollection optionalCollection, Object optionalCollectionOwner, Boolean returnProxies)
   at NHibernate.Loader.Loader.LoadCollection(ISessionImplementor session, Object id, IType type, Object owner, PersistentCollection collection)
   at NHibernate.Loader.OneToManyLoader.Initialize(Object id, PersistentCollection collection, Object owner, ISessionImplementor session)
   at NHibernate.Impl.SessionImpl.InitializeCollection(PersistentCollection collection, Boolean writing)
   at NHibernate.Collection.PersistentCollection.ForceInitialization()
   at NHibernate.Impl.SessionImpl.InitializeNonLazyCollections()
   at NHibernate.Loader.Loader.DoFindAndInitializeNonLazyCollections(ISessionImplementor session, QueryParameters parameters, Object optionalObject, Object optionalID, PersistentCollection optionalCollection, Object optionalCollectionOwner, Boolean returnProxies)
   at NHibernate.Loader.Loader.Find(ISessionImplementor session, QueryParameters parameters, Boolean returnProxies)
   at NHibernate.Loader.CriteriaLoader.List(ISessionImplementor session)
   at NHibernate.Impl.SessionImpl.Find(CriteriaImpl criteria)



I can't see what I'm doing wrong,

I get the same error if the MRPOutput.hbm.xml reads as below (unique-key attributes removed):

Code:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
   <class name="PerpetualInventory.MRPOutput, PerpetualInventory" table="SCF_TBL">   
   
      <id name="ID" column="SCF_UNIQUE" type="Int32" unsaved-value="0" access="field.pascalcase-m-underscore">
         <generator class="identity"/>
      </id>
         
      <property name="OrderDate" column="SCF_ORDER_DATE" type="DateTime" access="field.pascalcase-m-underscore"/>
            
      <many-to-one name="Part" class="PerpetualInventory.Part, PerpetualInventory"
                              access="field.pascalcase-m-underscore"
                               not-null="false" cascade="none">
               <column name="SCF_PART_ONLY" />
               <column name="SCF_PART_REV"  /> 
      </many-to-one>
      
      
   </class>

</hibernate-mapping>

[quote][/quote]


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 07, 2005 6:33 am 
Newbie

Joined: Tue Oct 04, 2005 6:20 am
Posts: 9
Figured it out

Code:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
   <class name="PerpetualInventory.Part, PerpetualInventory" table="PMA_TBL">   
   
      <!-- The table uses a the part only and part rev fields as a unique identifier -->
      <composite-id>
         <key-property name="PartOnly" column="PMA_PART_ONLY" access="field.pascalcase-m-underscore"/>
         <key-property name="PartRev" column="PMA_PART_REV" access="field.pascalcase-m-underscore"/>
      </composite-id>
            
      <!-- Map the table fields -->
      <property name="Desc" column="PMA_DESC" access="field.pascalcase-m-underscore"/>
      <property name="Desc_1" column="PMA_DESC_1" access="field.pascalcase-m-underscore"/>
      <property name="ProcurementCode" column="PMA_PROC_CODE" access="field.pascalcase-m-underscore"/>
      <property name="ABC" column="PMA_ABC_CODE" access="field.pascalcase-m-underscore"/>
      <property name="UOM" column="PMA_UOM" access="field.pascalcase-m-underscore"/>
      <property name="LastCheckDate" column="PMA_LAST_CHECK_DATE" access="field.pascalcase-m-underscore" not-null="false"/>
   
      <!--There is a one-to-many relationship between the Part and MRPOutput based upon the PartOnly
         and the PartRev-->      
      <bag name="MRPOutputs" inverse="true" lazy="true" access="field.pascalcase-m-underscore">
         <key>
            <column name="SCF_PART_ONLY"  unique-key="PMA_PART_ONLY"/>
            <column name="SCF_PART_REV"  unique-key="PMA_PART_REV"/>
         </key>
         <one-to-many class="PerpetualInventory.MRPOutput, PerpetualInventory"/>
      </bag>
         
      
      <!--There is a one-to-many relationship between the Part and Lot based upon the PartOnly
         and the PartRev-->
      <bag name="Lots" inverse="true" lazy="true" access="field.pascalcase-m-underscore">
         <key>
            <column name="LOT_PART_ONLY" unique-key="PMA_PART_ONLY"/>
            <column name="LOT_PART_REV" unique-key="PMA_PART_REV"/>
         </key>
         <one-to-many class="PerpetualInventory.Lot, PerpetualInventory"/>
      </bag>
      
   </class>

</hibernate-mapping>



It was to do with the entries for the column name and column unique-key attributes, it took a wild guess because it wasn't obvious reading the hibernate documents, but I get there.

The column name refers to the column in the child table, the unique key refers to the related column in this tables composite id


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