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...
|