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: Mapping composite key relationships
PostPosted: Mon May 28, 2007 1:23 am 
Newbie

Joined: Mon May 28, 2007 12:24 am
Posts: 1
Hi

I'm having trouble with mapping composite key relationships in a legacy database. If someone could give me a steer in the right direction for the relationship below, I'd really appreciate it.

This is a parent-child relationship, where the parent table defines an employee's Position History, and the child PositionDetails table holds more information about the positions.

Code:
Table:   PositionHistory
Columns:
   coy smallint [pk]
   emp_number int [pk]
   posn_history_date datetime [pk]
   posit_work_area char(4)
   posit_para smallint
   posit_line smallint
   posit_seq smallint
   posit_tsh_seq smallint
   audit_verify_time datetime
      
Table: PositionDetails
Columns:
   coy smallint [pk]
   posit_work_area char(4) [pk]
   posit_para smallint [pk]
   posit_line smallint [pk]
   posit_seq smallint [pk]
   posit_tsh_seq smallint [pk]
   posit_eff_date datetime [pk]
   designation varchar(40)
   audit_verify_time datetime


I want to populate a "PositionDetails" collection in my PositionHistory class.

I've tried countless variations in my mapping file, but whatever I try I come back to the same error: "Foreign key in table PositionDetails must have same number of columns as referenced primary key in table PositionHistory".

It looks like NHibernate is trying to map the Primary Key of PositionDetails to the Primary Key of PositionHistory, instead of using the posit_* columns.

The most recent version of my mapping file is:

Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Core" namespace="Core.Domain">
   <class name="PositionHistory" table="PositionHistory" lazy="true">
      <composite-id name="Pk" class="PositionHistoryPk">
         <key-property name="Company" column="coy" />
         <key-property name="EmpNumber" column="emp_number" />
         <key-property name="EffectiveDate" column="posn_history_date" />
      </composite-id>
         
      <version name="Timestamp" column="audit_verify_time" type="Timestamp" />

      <map name="Position" lazy="true" order-by="posit_eff_date">
         <key>
            <column name="coy" />
            <column name="posit_work_area" />
            <column name="posit_para" />
            <column name="posit_line" />
            <column name="posit_seq" />
            <column name="posit_tsh_seq" />
         </key>
         <index type="timestamp">
            <column name="posit_eff_date" />
         </index>
         <one-to-many class="PositionDetails" />
      </map>
   </class>
      
   <class name="PositionDetails" table="PositionDetails" lazy="true">
      <composite-id name="Pk" class="PositionPk">
         <key-property name="Company" column="coy" />
         <key-property name="WorkArea" column="posit_work_area" />
         <key-property name="Paragraph" column="posit_para" />
         <key-property name="Line" column="posit_line" />
         <key-property name="Sequence" column="posit_seq" />
         <key-property name="TshSeq" column="posit_tsh_seq" />
         <key-property name="EffectiveDate" column="posit_eff_date" />
      </composite-id>

      <version name="Timestamp" column="audit_verify_time" type="Timestamp" />

      <property name="Designation" column="designation" />
   </class>
</hibernate-mapping>


Please tell me what changes I need to make to this mapping file to populate the "PositionDetails" collection in my PositionHistory class?

Thanks,
Andrew


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 13, 2007 5:55 am 
Newbie

Joined: Thu Sep 13, 2007 4:55 am
Posts: 4
Hibernate only allow to set the same numbers like the given number of id.

In your case you must define your keys of the map to :

Code:
<class name="PositionHistory" table="PositionHistory" lazy="true">
      <composite-id name="Pk" class="PositionHistoryPk">
         <key-property name="Company" column="coy" />
         <key-property name="EmpNumber" column="emp_number" />
         <key-property name="EffectiveDate" column="posn_history_date" />
      </composite-id>
         
      <version name="Timestamp" column="audit_verify_time" type="Timestamp" />

      <map name="Position" lazy="true" order-by="posit_eff_date">
         <key>
            <column name="coy" column=""/>
            <column name="" />
            <column name="" />
         </key>

The key of the map is the same one as the given key.


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.