-->
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: one-to-many association: foreign key that has > 2 column
PostPosted: Wed Nov 30, 2005 4:46 pm 
Newbie

Joined: Wed Nov 30, 2005 3:45 pm
Posts: 10
Location: Vancouver. B.C.
I have a one-to-many association and the key is a composite of 2 fields in the DB and the PO objects. I get the error "Foreign key (SkillsetSiteAssociationAdmin [skillsetName])) must have same number of columns as the referenced primary key (SkillsetAdmin [skill ] setName,configID])" and am not sure what the syntax is to support this in the asscociation or if it is even possible.

Can someone please give me some input on these more complicated associations or give me a reference. I have found several other people struggling with this but have not seen a solution as yet.

Thanks, Alex.


Hibernate version:hibernate2.1.6.jar

Mapping documents:
/**
* @hibernate.set
* role="skillsetSiteAssociationsAdmin"
* cascade="all"
* lazy="false"
* @hibernate.collection-key
* column="skillsetName"
* column="configID" /*** Not sure of the syntax to add 2nd column***/
* @hibernate.collection-one-to-many
* class="com.telus.ccs.adminRouting.model.po.SkillsetSiteAssociationAdmin"
* not-null="true"
* unique="true"
*/
public Set getSkillsetSiteAssociationAdmin() {
return this.skillsetSiteAssociations;
}

Table Create:
***********
create table SkillsetAdmin (
configID number not null,
skillsetName varchar2(40) not null,
skillsetType varchar2(20) not null,
primary key (skillsetName, configID)
);

Table Constraint:
***************
alter table SkillsetSiteAssociationAdmin add constraint FKSiteREFSkillset foreign key (skillsetName, configID) references SkillsetAd
min;

Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:
##PEM Mapping resource: com/telus/ccs/adminRouting/model/po/SkillsetAdmin.hbm.xml
##PEM trying to locate http://hibernate.sourceforge.net/hibern ... ng-2.0.dtd in classpath under net/sf/hibernate/
##PEM found http://hibernate.sourceforge.net/hibern ... ng-2.0.dtd in classpath
[Loaded com.telus.ccs.adminRouting.model.po.SkillsetAdmin]
##PEM Mapping class: com.telus.ccs.adminRouting.model.po.SkillsetAdmin -> SkillsetAdmin
[Loaded com.telus.ccs.adminRouting.model.po.SkillsetAdminKey]
##PEM Mapped property: skillsetName -> skillsetName, type: string
##PEM Mapped property: configID -> configID, type: integer
[Loaded com.telus.ccs.adminRouting.model.po.SkillsetAdminKey$$FastClassByCGLIB$$bf282571]
[Loaded com.telus.ccs.adminRouting.model.po.SkillsetAdminKey$$BulkBeanByCGLIB$$2c9c95c7]
##PEM Mapped property: id -> skillsetName, configID, type: com.telus.ccs.adminRouting.model.po.SkillsetAdminKey
##PEM Mapped property: skillsetType -> skillsetType, type: string
[Loaded com.telus.ccs.adminRouting.model.po.SkillsetSiteAssociationAdmin]
##PEM Mapped property: skillsetSiteAssociationAdmin, type: java.util.Set

#PEM null<-org.dom4j.tree.DefaultAttribute@57c2bd [Attribute: name resource value "com/telus/ccs/adminRouting/model/po/SkillsetSiteAssociationAdmin.hbm.xml"]
##PEM Mapping resource: com/telus/ccs/adminRouting/model/po/SkillsetSiteAssociationAdmin.hbm.xml
##PEM trying to locate http://hibernate.sourceforge.net/hibern ... ng-2.0.dtd in classpath under net/sf/hibernate/
##PEM found http://hibernate.sourceforge.net/hibern ... ng-2.0.dtd in classpath
##PEM Mapping class: com.telus.ccs.adminRouting.model.po.SkillsetSiteAssociationAdmin -> SkillsetSiteAssociationAdmin
##PEM Mapped property: recordID -> recordID, type: integer
##PEM Mapped property: skillsetName -> skillsetName, type: string
##PEM Mapped property: siteName -> siteName, type: string
##PEM Mapped property: percentage -> percentage, type: integer
##PEM Mapped property: phoneNumber -> phoneNumber, type: string
##PEM Mapped property: configID -> configID, type: integer
##PEM processing one-to-many association mappings
##PEM Second pass for collection: com.telus.ccs.adminRouting.model.po.Skillset.skillsetSiteAssociations
##PEM Mapping collection: com.telus.ccs.adminRouting.model.po.Skillset.skillsetSiteAssociations -> SkillsetSiteAssociation
##PEM Mapped collection key: skillsetName, one-to-many: com.telus.ccs.adminRouting.model.po.SkillsetSiteAssociation
##PEM Second pass for collection: com.telus.ccs.adminRouting.model.po.SkillsetAdmin.skillsetSiteAssociationAdmin
##PEM Mapping collection: com.telus.ccs.adminRouting.model.po.SkillsetAdmin.skillsetSiteAssociationAdmin -> SkillsetSiteAssociationAdmin
##PEM Mapped collection key: skillsetName, one-to-many: com.telus.ccs.adminRouting.model.po.SkillsetSiteAssociationAdmin
##PEM processing one-to-one association property references
##PEM processing foreign key constraints
[Loaded java.util.HashMap$2 from /bea/jdk131_06/jre/lib/rt.jar]
##PEM resolving reference to class: com.telus.ccs.adminRouting.model.po.SkillsetAdmin
[Loaded net.sf.hibernate.exception.NestableDelegate]
[Loaded com.telus.ccs.common.storage.TLStorage]
[Loaded com.telus.ccs.common.storage.TLStorage$TL]
[Loaded org.apache.commons.lang.time.DateFormatUtils]
[Loaded org.apache.commons.lang.time.FastDateFormat]
[Loaded org.apache.commons.lang.time.FastDateFormat$Rule]
[Loaded org.apache.commons.lang.time.FastDateFormat$NumberRule]
[Loaded org.apache.commons.lang.time.FastDateFormat$TextField]
[Loaded org.apache.commons.lang.time.FastDateFormat$UnpaddedNumberField]
[Loaded org.apache.commons.lang.time.FastDateFormat$Pair]
[Loaded org.apache.commons.lang.time.FastDateFormat$CharacterLiteral]
[Loaded org.apache.commons.lang.time.FastDateFormat$TwoDigitMonthField]
[Loaded org.apache.commons.lang.time.FastDateFormat$TwoDigitNumberField]
[Loaded org.apache.commons.lang.time.FastDateFormat$TimeZoneNumberRule]
[Loaded java.text.resources.LocaleElements_en_US from /bea/jdk131_06/jre/lib/rt.jar]
[Loaded java.util.ResourceBundle$1 from /bea/jdk131_06/jre/lib/rt.jar]
[Loaded org.apache.commons.lang.time.FastDateFormat$StringLiteral]
##PEM 2005.11.30-13:35:09 ivrapp02.ent.agt.ab.ca UNTITLED FATAL com.telus.ccs.util.DAO [ -1 Failed to obtain hibernate session:
net.sf.hibernate.MappingException Foreign key (SkillsetSiteAssociationAdmin [skillsetName])) must have same number of columns as the referenced primary key (SkillsetAdmin [skill ] setName,configID])
[Loaded java.lang.ExceptionInInitializerError from /bea/jdk131_06/jre/lib/rt.jar]
Exception in thread "main" java.lang.ExceptionInInitializerError: com.telus.ccs.base.DataAccessException: null; nested exception is net.sf.hibernate.MappingException: Foreign key (SkillsetSiteAssociationAdmin [skillsetName])) must have same number of columns as the referenced primary key (SkillsetAdmin [skillsetName,configID])
at com.telus.ccs.util.SessionFactoryUtil.<clinit>(SessionFactoryUtil.java:29)
at com.telus.ccs.util.DAO.read(DAO.java:95)
at test.ConfigDAOtest.main(ConfigDAOtest.java:36)
[Loaded java.lang.Shutdown$Lock from /bea/jdk131_06/jre/lib/rt.jar]




Name and version of the database you are using:
Oracle 9.0

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 30, 2005 7:33 pm 
Regular
Regular

Joined: Wed May 05, 2004 3:41 pm
Posts: 118
Location: New Jersey,USA
Here's something that might work. Off course these are the mapping XMLs. Not sure how the translate into Java annotations:

Code:
<hibernate-mapping>
    <class name="com.hibernate.forums.SkillsetAdmin"
           table="SkillsetAdmin"
           schema="ANARASI"
           >
        <composite-id>
            <key-property name="configID"/>
            <key-property name="skillsetName"/>
        </composite-id>
        <property name="skillsetType" type="string">
            <column name="skillsetType"/>
        </property>   
      <set    name="admins"
            cascade="all-delete-orphan"
            inverse="true"
            lazy="true"
            access="field">
           <key>
               <column name="configID" not-null="true"/>
               <column name="skillsetName" not-null="true"/>
           </key>
           <one-to-many class="com.hibernate.forums.SkillsetSiteAssociationAdmin"/>
      </set>           
    </class>
</hibernate-mapping>


For the child class:
Code:

<hibernate-mapping>
   <class name="com.hibernate.forums.SkillsetSiteAssociationAdmin"
           table="SkillsetSiteAssociationAdmin"
           schema="ANARASI"
           >
        <id name="assocId" type="integer">
            <column name="assocID" />
            <generator class="assigned" />
        </id>
        <property name="assocDesc" type="string">
            <column name="assocDesc"/>
        </property>   
       <many-to-one    name="skillSetAdmin"
                       insert="false"
                       update="false"
                       not-null="true"
                       class="com.hibernate.forums.SkillsetAdmin">       
             <column name="configId"></column>
             <column name="skillsetName"></column>
       </many-to-one>
    </class>
</hibernate-mapping>


Obviously you will need to determine what kind of associations you would need based on your application.

Refer the CaveatEmptor example at:
http://caveatemptor.hibernate.org/


Top
 Profile  
 
 Post subject: XDoclet Syntax/Limitations
PostPosted: Thu Dec 01, 2005 3:22 am 
Newbie

Joined: Wed Nov 30, 2005 3:45 pm
Posts: 10
Location: Vancouver. B.C.
Thanks! Editting the generated XML document and putting in the correct syntax for the KEY worked. I read XDoclet may have some limitations with building Composite Keys so this may be the case here with associations. For now I will use the modified mapping file and try and get an answer with regards to XDoclet.

Your speedy help is MUCH appreciated!

Alex.


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.