0

Problem Summary

I am trying to map a many-to-one with a composite key. So far I haven't been able to locate another question that helps. I know how to map to a composite key with one-to-one, but it is not allowing me to map many-to-one.

Below you will see bhrvJournalDAO has a store_nbr column, that maps to transAcctgBuTxtDAO acctg_bu_id. How can I hit the composite key and tell it to only use the acctgBuId? I do not have a country code (which is the second part of the composite key).

From Here

<hibernate-mapping>
<class name="com.acctg.BhrvJournalDAO" table="transpo_acctg:bhrv_journal">
    <id name="jeId" column="je_id">
        <generator class="native"/>
    </id>
    <property name="bhrvInvoiceId" column="bhrv_invoice_id"/>
    <property name="storeNbr" column="store_nbr"/>
    <property name="loadId" column="load_id"/>

  <many-to-one name="transAcctgBuTxt" class="TransAcctgBuTxtDAO" insert="false" update="false" cascade="all">
              <column name="store_nbr"></column>
  </many-to-one>
</class>

To Here

<hibernate-mapping>
<class name="TransAcctgBuTxtDAO"    table="transpo_acctg:trans_acctg_bu_txt">
    <composite-id name="transAcctgBuTxtPKDAO"   class="TransAcctgBuTxtPKDAO">
        <key-property name="acctgBuId" column="acctg_bu_id"/>
        <key-property name="languageCode" column="language_code"/>
    </composite-id>
    <property name="acctgBuAbbr" type="java.lang.String">
        <column name="acctg_bu_abbr" />
    </property>
    <property name="acctgBuDesc" type="java.lang.String">
        <column name="acctg_bu_desc" />
    </property>
    <many-to-one name="transAcctgBuDAO"  class="TransAcctgBuDAO" not-null="false" 
        insert="false" update="false" not-found="ignore" fetch="select">
        <column name="acctg_bu_id" />
    </many-to-one>
 </class>

Mapping

  <many-to-one name="transAcctgBuTxt" class="TransAcctgBuTxtDAO" 
        insert="false" update="false" cascade="all">
              <column name="store_nbr"></column>
  </many-to-one>

Composite Key Example

 <one-to-one name="transAcctgBuTxt" class="TransAcctgBuTxtDAO" property-
       ref="transAcctgBuTxtPKDAO.acctgBuId"> 
        <column name = "store_nbr">
 </one-to-one>

Error

Foreign key (FK47A121BB6617227C:transpo_acct:bhrv_journal [store_nbr])) 
must have same number of columns as the referenced primary 
key (transpo_acct:trans_acctg_bu_txt [acctg_bu_id,language_code])

Thanks in advance

zach
  • 1,281
  • 7
  • 27
  • 41

1 Answers1

0

Since TransAcctgBuTxtDAO has the composite id of 2 columns, you NEED to provide both values to uniquely identify one entity, period. Maybe you could reconsider if it is really a many-to-one relation between BhrvJournalDAO and TransAcctgBuTxtDAO. Mabye it is actually many-to-many.

Chin Hung
  • 66
  • 3
  • @Chin_Hung. That is kind of what i was worried about. And unfortunately it is a table referring to a refence table (we store a list of definations with keys that are used in BhrvJournal). I ended up just using another query to fetch what i needed. Not be best approach, but you confirmed that it won't work with my setup. Thanks for you time – zach Apr 02 '13 at 19:51