-->
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: Parent child mapping with composite ids
PostPosted: Tue Feb 08, 2005 5:22 am 
Newbie

Joined: Sun Jul 25, 2004 8:12 am
Posts: 4
Hibernate version: Hibernate2 HEAD 2005-02-08

Hi,

I am trying to map from a parent table to a child table with composite Ids. I am not using a unique key because the tables are intended to be updated manually. It is easier to add new pages by adding the feature number and the page number than it is to remember a generated unique key.

The relationship is Feature -> Page -> Section. This is my mapping document:

<hibernate-mapping package="model">

<class name="Feature" table="feature" mutable="false">
<id name="featureNumber" unsaved-value="0" column="feature_number">
<generator class="assigned"/>
</id>
<property name="name"/>
<property name="description"/>
<list name="pages" lazy="true">
<key>
<column name="feature_number"/>
<column name="page_number"/>
</key>
<index column="page_number"/>
<one-to-many class="Page"/>
</list>
</class>

<class name="Page" table="page" mutable="false">
<composite-id>
<key-many-to-one name="feature" class="Feature"/>
<key-property name="pageNumber" column="page_number"/>
</composite-id>
<property name="title"/>
<property name="description"/>
<list name="sections" lazy="true">
<key>
<column name="feature_number"/>
<column name="page_number"/>
<column name="section_number"/>
</key>
<index column="section_number"/>
<one-to-many class="Section"/>
</list>
</class>

<class name="Section" table="section" mutable="false">
<composite-id>
<key-many-to-one name="page" class="Page">
<column name="feature_number"/>
<column name="page_number"/>
</key-many-to-one>
<key-property name="sectionNumber" column="section_number"/>
</composite-id>
<property name="title"/>
<property name="description"/>
<property name="image"/>
<property name="position"/>
</class>

</hibernate-mapping>

And this is the message

net.sf.hibernate.MappingException: Foreign key (section [feature_number,page_number,section_number])) must have same number of columns as the referenced primary key (page [feature,page_number])
at net.sf.hibernate.mapping.ForeignKey.setReferencedTable(ForeignKey.java:67)
...

Any suggestions on how to map this kind of relationship?

Thanks,

John.


Top
 Profile  
 
 Post subject: composite keys
PostPosted: Tue Feb 08, 2005 12:17 pm 
Newbie

Joined: Mon Feb 07, 2005 3:17 pm
Posts: 4
John,

I am getting this same error, and I posted the question yesterday with no response. From everything that I have seen, it appears to be a 'feature' of Hibernate that mapping can't be done to a child table unless the keys match.


Top
 Profile  
 
 Post subject: Problem solved
PostPosted: Tue Feb 08, 2005 4:44 pm 
Newbie

Joined: Sun Jul 25, 2004 8:12 am
Posts: 4
Opps. I just realised that I shouldn't have included the full foreign key in the lists <key> but only the parts that relate back to the parent table. Posted too soon!

Here is the correct mapping:

<?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 package="model">

<class name="Feature" table="feature" mutable="false">
<id name="featureNumber" unsaved-value="0" column="feature_number">
<generator class="assigned"/>
</id>
<property name="name"/>
<property name="description"/>
<list name="pages" lazy="true">
<key>
<column name="feature_number"/>
</key>
<index column="page_number"/>
<one-to-many class="Page"/>
</list>
</class>

<class name="Page" table="page" mutable="false">
<composite-id>
<key-many-to-one name="feature" class="Feature"/>
<key-property name="pageNumber" column="page_number"/>
</composite-id>
<property name="title"/>
<property name="description"/>
<list name="sections" lazy="true">
<key>
<column name="feature_number"/>
<column name="page_number"/>
</key>
<index column="section_number"/>
<one-to-many class="Section"/>
</list>
</class>

<class name="Section" table="section" mutable="false">
<composite-id>
<key-many-to-one name="page" class="Page">
<column name="feature_number"/>
<column name="page_number"/>
</key-many-to-one>
<key-property name="sectionNumber" column="section_number"/>
</composite-id>
<property name="title"/>
<property name="description"/>
<property name="image"/>
<property name="position"/>
</class>

</hibernate-mapping>


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.