-->
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: MANY-TO-ONE Association with composite keys....???
PostPosted: Wed Nov 24, 2004 8:18 am 
Newbie

Joined: Sun Nov 21, 2004 6:20 pm
Posts: 11
Hi,

I still have problems to model associations between tables with composite keys....HOW CAN I MODEL
A MANY-TO-ONE ASSOCIATION TO A TABLE WITH COMPOSITE-ID????????????????????????????

I have the following tables:

table Question
question_id (pk)
question_text

table Option
option_id (pk)
question_id (pk and fk referencing table Question)

table Vote
vote_id (pk)
option_id (fk to table Option)
question_id (fk to table Option)

According to the Hibernate Reference chapter 5.1.5 and 7.4 I implemented the primary key of table Option in an additional class OptionIdentifyer which wraps the to properties..(As I do not want to
have this class mapped to a database table there is no OptionIdentifyer.hbm.xml)..I fed hbm2java with the mapping-files for Question and Option and it generated reasonable-looking classes. The
SchemaUpdate-Tool generated good table definitions.

Now I wrote the mapping-file for the class/table Vote like this:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernater/Hibernate Mapping DTD//EN"
"http://127.0.0.1/ryll/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
<class name="de.ryll.hibernate.Vote"
table="Vote">
<id name="vote_id" column="vote_id" type="integer">
<generator class="assigned"/>
</id>
<property name="datum" column="datum" type="date"/>
<many-to-one name="option_id" column="option_id" class="de.ryll.hibernate.Options">
</many-to-one>
</class>
</hibernate-mapping>

But of course the SchemaUpdate-tool did not like it and threw the following messages:
12:48:45,440 ERROR SchemaUpdate:147 - could not complete schema update
net.sf.hibernate.MappingException: Foreign key (Vote [option_id])) must have same number of columns as the referenced primary key (Options [option_id,question_id])
at net.sf.hibernate.mapping.ForeignKey.setReferencedTable(ForeignKey.java:60)
at net.sf.hibernate.cfg.Configuration.secondPassCompileForeignKeys(Configuration.java:691)
at net.sf.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:666)
at net.sf.hibernate.cfg.Configuration.generateSchemaUpdateScript(Configuration.java:550)
at net.sf.hibernate.tool.hbm2ddl.SchemaUpdate.execute(SchemaUpdate.java:126)
at de.ryll.hibernate.UseHib4Person.generateDDL(UseHib4Person.java:37)
at de.ryll.hibernate.UseHib4Person.main(UseHib4Person.java:25)

************
Obviously, I declared only one Column for the foreign key, but the primary key of table Option
consists of two columns.....but what can I do??....I am not allowed to define another column-field
within the <many-to-one> - element and a line like:
<many-to-one name="option_id" column="option_id" class="de.ryll.hibernate.OptionIdentifyer">
</many-to-one>
does not work either as there is no mapping-file for this class...But I do not want to have a
mapping-file for this class as I do not want to have this class to be persistent in the database...
It is just a HelperClass for Hibernate and I do not want to have it in my database.....


THANKS for any help...as I really do not know what to do...


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 24, 2004 12:46 pm 
Regular
Regular

Joined: Tue Oct 26, 2004 3:54 pm
Posts: 60
Hi Max,

use this syntax :


<many-to-one>
<column name="name" />
<column name="othername" />
...
</many-to-one>

As explained in chapter 7.4 of Hibernate Reference Manual

Giulio Vezzelli


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.