-->
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: Composite key and one-to-many relationship
PostPosted: Mon Oct 20, 2003 4:28 am 
Beginner
Beginner

Joined: Wed Aug 27, 2003 6:15 am
Posts: 22
Hi all

The first table, CMM_CODE_VALUE, has a single-column PK called CODE_VALUE_ID. This table is on the many-side of the relationship.

The other table, CMM_SYSTEM_CODE, has a composite key of 2 columns, SYSTEM_CODE and COMPANY_ID, which have been mapped using a separate class called SystemCodePK. This table is on the one-side of the relationship.

I would like to know how I should write my mapping file. My mapping files (attached below) are giving me a MappingException on startup:

Code:
Exception in thread "main" net.sf.hibernate.MappingException: Foreign key must have same number of columns as referenced primary key
        at net.sf.hibernate.mapping.ForeignKey.setReferencedTable(ForeignKey.java:33)
        at net.sf.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:523)
        at net.sf.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:623)
        at com.abc.common.dr.cmm.Test.main(Test.java:19)


Also, how do I map the child relationship using <set>, given that <set> only allows one <key> element and there are now 2 key columns?

Thanks a lot for your help.

--- Child table mapping file ---
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
    <class
        name="com.abc.common.dr.cmm.CodeValueImpl"
        table="CMM_CODE_VALUE"
        proxy="com.abc.common.dr.cmm.CodeValueORInterface"
        dynamic-update="false"
        dynamic-insert="false"
    >
        <id
            name="codeValueId"
            column="CODE_VALUE_ID"
            type="long"
        >
            <generator class="sequence">
          <param name="sequence">CMM_CODE_VALUE_ID_SQ</param>
            </generator>
        </id>
        <!-- other properties deleted for brevity -->
        <many-to-one
            name="parentCodeValue"
            class="com.abc.common.dr.cmm.CodeValueImpl"
            cascade="none"
            outer-join="auto"
            update="true"
            insert="true"
            column="PARENT_CODE_VALUE_ID"
        />
        <many-to-one
            name="companyIdsystemCode"
            class="com.abc.common.dr.cmm.SystemCodeImpl"
            cascade="none"
            outer-join="auto"
            update="true"
            insert="true"
        >
      <column name="SYSTEM_CODE"/>
      <column name="COMPANY_ID"/>
   </many-to-one>
        <set
            name="codeValuesByParentCodeValue"
            table="CMM_CODE_VALUE"
            lazy="true"
            inverse="false"
            cascade="none"
            sort="unsorted"
        >
              <key
                  column="PARENT_CODE_VALUE_ID"
              />
              <one-to-many
                  class="com.abc.common.dr.cmm.CodeValueImpl"
              />
        </set>
        <!--
            To add non XDoclet property mappings, create a file named
                hibernate-properties-CodeValueImpl.xml
            containing the additional properties and place it in your merge dir.
        -->
    </class>
</hibernate-mapping>


--- Parent table mapping file ---
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
    <class
        name="com.abc.common.dr.cmm.SystemCodeImpl"
        table="CMM_SYSTEM_CODE"
        proxy="com.abc.common.dr.cmm.SystemCodeORInterface"
        dynamic-update="false"
        dynamic-insert="false"
    >
        <composite-id
            name="compositePrimaryKey"
            class="com.abc.common.dr.cmm.SystemCodePK"
        >
                     <key-property
                        name="systemCode"
                        type="string"
                        column="SYSTEM_CODE"
                />
                     <key-property
                        name="company"
                        type="double"
                        column="COMPANY_ID"
                />
        </composite-id>
        <!-- other properties deleted for brevity -->
        <set
            name="codeValuesByCompanyIdsystemCode"
            table="CMM_CODE_VALUE"
            lazy="true"
            inverse="false"
            cascade="none"
            sort="unsorted"
        >
              <key
                  column="COMPANY_ID"
              />
              <one-to-many
                  class="com.abc.common.dr.cmm.CodeValueImpl"
              />
        </set>
        <!--
            To add non XDoclet property mappings, create a file named
                hibernate-properties-SystemCodeImpl.xml
            containing the additional properties and place it in your merge dir.
        -->
    </class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject: Source code with this working
PostPosted: Mon Oct 20, 2003 7:23 pm 
Newbie

Joined: Sun Oct 12, 2003 9:53 pm
Posts: 13
Location: Buenos Aires, Argentina
I'm working in a prj that has this: composite keys with one to many relationships. You can find it on jagzilla.sourceforge.net, there's a link to cvs to view online, navigate to net.sf.jagzilla.hibernate There are all the java classes. Sorry that I don't post all the files related, but the package I mentioned is quite big.
Hope it helps


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.