Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

One-to-Many in a class having composite primary key

 
Pratheek Patel
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Iam having a table which is having three fields.
Table name: REGION_AUCTION
composite primary key: auction_id + region_id + auction_loc
one more column named: auc_rep_id --> mapped to user_id in USERS table.

This table is refering to another table(Users).
Table name : USERS
primary key: user_id

And there is One-to-Many relationship between REGION_AUCTION and USERS.

Getting a MappingException saying that "Foriegn Key(user_id) must be defined for the referenced primary key(auction_id,region_id,auction_loc)"

How can i write a <set> tag in REGION_AUCTION.hbm.xml to refer USERS table and a corresponding <many-to-one> tag in USERS.hbm.xml.

Please help me out to resolve this problem. Thanks in advance...
 
Anirudh Vyas
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Create a the class for the mapping like

public class ClassWithCompositeId implements Serializable{


public static class Id implements Serializable {
// your Id fields go here ...

}


// .... blah blah


}

ok you map your composite id class like this : ( this is the class which has the set ( notice the '$' notation, thats an inner class for Id mapping )

<composite-id name="id" class="ClassWithCompositeId$YOUR_COMPOSITE_ID_CLASS_HERE">
<key-column name="" blah blah column="" blah blah />
<key-column name="" blah blah column="" blah blah />
<key-column name="" blah blah column="" blah blah />
</composite-id>

<set name="" cascade="save-update">
<key column="id" />
<one-to-many class="Users" />
</set>

there you go ... if you need further clarifications i'd be happy to help ...

Regards
Vyas, Anirudh
[ February 18, 2008: Message edited by: Anirudh Vyas ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic