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.  [ 2 posts ] 
Author Message
 Post subject: What is the best composite primary key strategy?
PostPosted: Fri May 23, 2008 6:17 am 
Newbie

Joined: Fri May 23, 2008 5:43 am
Posts: 13
Location: Switzerland
Hi,

I never used composite primary key before, but now I have a situation, which needs one. I have 3 questions:
1. Which is the best strategy of the following available in Hibernate (EJB3), what are advantages/disadvantages of each?

annotate the component property as @Id and make the component class @Embeddable
annotate the component property as @EmbeddedId
annotate the class as @IdClass and annotate each property of the entity involved in the primary key with @Id

2. Is there a way to create this key class in the same file as the main class? Creating it as inner class is not allowed - it sould be top level class, is there any way around this?

3. Any better strategy than creating a key class for every single class that I have?

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 23, 2008 9:28 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
My personal preference has been to use the @IdClass, but each scenario has specific curcumstances which may effect your choice.

Code:
@Entity
@IdClass(com.examscam.mappings.CompoundKey.class)
public class Fracture {



Here's a good tutorial on how to map compound primary keys with Hibernate and JPA:

http://www.hiberbook.com/HiberBookWeb/learn.jsp?tutorial=15usingcompoundprimarykeys

I always make my primary key classes toplevel classes, and not inner classes, or even non-public classes within the same Java class file as the associated instance. I recommend that people using composite primary keys do the same.

Good luck!

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.