-->
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: Problem with many-to-many relationships that have also rows
PostPosted: Fri Apr 13, 2007 12:59 pm 
Newbie

Joined: Fri Apr 13, 2007 12:54 pm
Posts: 8
Hello

I am having a problems mapping a many-to-many association where the middle table also has fields other than the primary keys of the other two tables.

The tables are:

Purchase (PK -> purchase_id)
Item (PK -> item_id)
ItemPurchase (PKs -> item_id, purchase_id)

The ItemPurchase table also has the column "quantity".

I also have the corresponding beans for these tables.

On the Hibernate In Action Book I read that you can make a mapping for the ItemPurchase table and define two many-to-one associations. What I don't get is how would I map the primary keys of the table. Here is my current hibernate mapping for this class.

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="controldeutiles.model.businessobject">
  <class name="ItemPurchase">
     <id></id>
     <many-to-one name="purchase" column="purchase_id" class="Purchase" not-null="true" />
     <many-to-one name="item" column="item_id" class="Item" not-null="true" />
     <property name="quantity" type="integer"></property>
     <property name="unit_amount" type="double" />
     <property name="total_amount" type="double" />
  </class>
</hibernate-mapping>




What should go between the "id" tags?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 13, 2007 2:48 pm 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
If there is no primary key on the table, use a composite id based on the purchase_id and item_id fields, see http://www.hibernate.org/hib_docs/v3/re ... ompositeid for more details.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 13, 2007 2:59 pm 
Newbie

Joined: Fri Apr 13, 2007 12:54 pm
Posts: 8
Thanks for your help, this really worked.


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.