-->
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.  [ 5 posts ] 
Author Message
 Post subject: Composite key's problem : Foreign key must have same ...
PostPosted: Mon Sep 01, 2003 3:54 am 
Beginner
Beginner

Joined: Wed Aug 27, 2003 3:46 am
Posts: 34
Location: Taiwan
Composite key's problem : Foreign key must have same number of columns as referenced primary key

In this scenario :

ClassA has two composite keys : key1 and key2
ClassA's instance may have zero or more ClassB.
ClassB has three composite keys : key1 , key2 and key3
ClassA has a method getClassBSet() to get the containing ClassB.


In the mapping :

Code:
  <class name="foo.ClassA" table="ClassA">
    <composite-id>
      <key-property name="key1" type="java.lang.String"/>
      <key-property name="key2" type="long"/>
    </composite-id>
    <set name="ClassBSet" table="ClassB" cascade="all" order-by="key3 asc">
         <key column="key1"/>
         <key column="key2"/>
         <one-to-many class="foo.ClassB"/>
      </set>
  </class>
 
  <class name="foo.ClassB" table="ClassB">
    <composite-id>
      <key-property name="key1" type="java.lang.String"/>
      <key-property name="key2" type="long"/>
      <key-property name="key3" type="long"/>
    </composite-id>
  </class>



But when Hibernate is mapping collection , some error occurs :

Error while initialize foo HibernateSession's sessionFactory
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:514)
at net.sf.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:614)


It seems that Hibernate requires that ClassB must have identical foreign keys to ClassA's primary key , isn't it ?
But in this scenario , it seems inevitalbe....
Can somebody solve this problem ?

I am using Hibernate 2.0.2


I know Hibernate discourages us to use composite keys ,
it encourage us to 'combine' composite keys to one independent class.
But for some historical reason , I have to design this way.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 01, 2003 4:00 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Your mapping looks perfect to me. I don't think the bit you just showed is what is causing the exception. I bet you have other stuff you are hiding from me :)


Top
 Profile  
 
 Post subject: Hibernate 2.1 beta2 output...
PostPosted: Mon Sep 01, 2003 5:19 am 
Beginner
Beginner

Joined: Wed Aug 27, 2003 3:46 am
Posts: 34
Location: Taiwan
gavin wrote:
Your mapping looks perfect to me. I don't think the bit you just showed is what is causing the exception. I bet you have other stuff you are hiding from me :)


Hi , I cannot find what information I hide ....
But ... after I migrate to Hibernate 2.1 beta 2 ,
the error messages are different :

First , it says that the second key int the Set is not allowed :
Code:
      <set name="ClassBSet" table="ClassB" cascade="all" order-by="key3 asc">
         <key column="key1"/>
         <key column="key2"/>      (This is Line 22)
         <one-to-many class="foo.ClassB"/>
      </set>



Error parsing XML: XML InputStream(22)
org.xml.sax.SAXParseException: 元件 "set" 在此不允許 "key"。
(it is big5 output message , means 'key' is not allowed in the element 'set')
at org.apache.crimson.parser.Parser2.error(Parser2.java:3160)
at org.apache.crimson.parser.ValidatingParser$ChildrenValidator.consume(ValidatingParser.java:349)
at org.apache.crimson.parser.Parser2.maybeElement(Parser2.java:1317)
at org.apache.crimson.parser.Parser2.content(Parser2.java:1779)


INFO: processing one-to-many association mappings
2003/9/1 05:03:48 net.sf.hibernate.cfg.Binder bindCollectionSecondPass
INFO: Mapping collection: foo.ClassA.ClassBSet -> ClassB
2003/9/1 05:03:48 net.sf.hibernate.cfg.Configuration secondPassCompile
INFO: processing one-to-one association property references
2003/9/1 05:03:48 net.sf.hibernate.cfg.Configuration secondPassCompile
INFO: processing foreign key constraints
Error while initialize foo HibernateSession's sessionFactory

net.sf.hibernate.MappingException: Foreign key (ClassB [key2])) must have same number of columns as the reference primary key (ClassA [key1,key2])

at net.sf.hibernate.mapping.ForeignKey.setReferencedTable(ForeignKey.java:56)
at net.sf.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:557)
at net.sf.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:651)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 01, 2003 6:26 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Oh woops!


I didn't notice that the XML was invalid!

Should be:


Code:
<key>
    <column name="key1"/>
    <column name="key2"/>
</key>



Always check the Hibernate log for validatin errors!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 01, 2003 8:45 am 
Beginner
Beginner

Joined: Wed Aug 27, 2003 3:46 am
Posts: 34
Location: Taiwan
Thank you , it solved !!!


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