-->
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: Composite foreign key mapping in Hibernate 3
PostPosted: Thu Jan 13, 2011 7:32 am 
Newbie

Joined: Thu Jan 13, 2011 7:23 am
Posts: 1
Hi All,

We have a table
A(a_id, name) where a_id is primary key
B(b_id, name) where b_id is primary key
C(a_id, b_id) where a_id & b_id are composite key and foreign key to table 'A' & 'B'

We have proper mapping for table 'A' & 'B' in hibernate 3 using annotation.

But we are not sure how to create entity class for table 'C', since it does not have any primary key.
We wanted to know how to map composite foreign key.

Can any one please help us how to create the entity class for table 'C'

Regards,
Sujit Joshi


Top
 Profile  
 
 Post subject: Re: Composite foreign key mapping in Hibernate 3
PostPosted: Fri Jan 14, 2011 12:35 pm 
Newbie

Joined: Fri Jan 14, 2011 12:25 pm
Posts: 2
you will have to create a Embeddable class something like

@Embeddable
public class PK implements Serializable {
String a_id;
String b_id;
}

then reference it from your entity class

@Id
public PK id;


Top
 Profile  
 
 Post subject: Re: Composite foreign key mapping in Hibernate 3
PostPosted: Fri Jan 14, 2011 7:08 pm 
Newbie

Joined: Mon Jan 10, 2011 7:21 pm
Posts: 8
Seems like you are trying to implement a many-to-many association?
If that's the case, I'd suggest you have a look at the @ManyToMany annotation and Hibernate will take care of generating all the tables/keys for you.

Regards,
- Savvas


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.