-->
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.  [ 7 posts ] 
Author Message
 Post subject: Composite Primary Key
PostPosted: Thu Dec 01, 2005 12:49 pm 
Newbie

Joined: Thu Dec 01, 2005 12:23 pm
Posts: 19
Hi there,

I'm wondering if there is a way to create a composite primary key for a database table using Hibernate Annotations, without having to create an @Embeddable primary key class? Or using an "artificial" id as primary key and then only creating a second unique constraint with the two columns!?

For exmaple for the table below I'd like to have "col1" and "col2" as the composite primary key (for whatever reason).

col1 | col2 | col3 | col4 | col5

I was expecting that following annotations would do it...

Code:
@Entity(access=AccessType.FIELD)
@Table(name="MYTABLE")
public MyClass {
  @Id
  private String col1;
  @Id
  private String col2;
  private String col3;
  private String col4;
  private String col5;
}


I should mention that "col1" is a foreign key and points to a primary key of an other table.

Unfortunately this does not work with (Hibernate) annotations. Only "col1" is used as primary key in this case.

I tried several ways but did not find any working solution. Even the way as described in the EJB3.0 specification does not work:

Code:
@Entity(access=FIELD)
@IdClass(com.acme.EmpPK.class)
public class Employee {
   @Id Integer id;
   @Id String name;
   @OneToOne
   @PrimaryKeyJoinColumns({
      @PrimaryKeyJoinColumn(name="ID", referencedColumnName="EMP_ID"), @PrimaryKeyJoinColumn(name="NAME", referencedColumnName="EMP_NAME")})
EmployeeInfo info;
...
}


The annotation @PrimaryKeyJoinColumns is not allowed within a class. And annotating the class with @PrimaryKeyJoinColumns also does not work.

So do I really need an @Embeddable primary key class to use a composite primary key?

Any help/info is appreciated.

- bitbyter


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 01, 2005 6:44 pm 
Regular
Regular

Joined: Wed Jun 30, 2004 4:02 pm
Posts: 64
I've always used a many to one with a unique constraint for the foreign key column.

Hibernate itself doesn't work very well with composite keys. Without a 3NF schema you have to use a version column for saveOrUpdate to work. Given that Annotations are not time tested like the xml mappings, I think you are asking for a painful experience trying to do this.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 01, 2005 7:36 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
For 2. Try
Code:
@OneToOne
@PrimaryKeyJoinColumn
EmployeeInfo info;


For 1. I need to implement that, I'm a bit bored on this :-) Any patch would be welcome.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 02, 2005 2:59 am 
Newbie

Joined: Thu Dec 01, 2005 12:23 pm
Posts: 19
emmanuel wrote:
For 2. Try
Code:
@OneToOne
@PrimaryKeyJoinColumn
EmployeeInfo info;


The @PrimaryKeyJoinColumn annotation is not allowed within a class, annotating a class member. It is only allowed to annotate a class itself. At least it does not compile with the Hibernate annotations implementation.

Quote:
For 1. I need to implement that, I'm a bit bored on this :-) Any patch would be welcome.


Gee, unfortunately I currently got no time... and I guess it would take me at least 1-2 days to get into the code. :( Anyway, now at least I know that this is a missing "feature".

- bitbyter


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 02, 2005 6:00 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
2. definitely compile. Double check. It's on the unit test suite.
Becareful it's wo the extra s

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 02, 2005 6:05 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
[quote]Hibernate itself doesn't work very well with composite keys. Without a 3NF schema you have to use a version column for saveOrUpdate to work. [quote]

You are mistaken.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 25, 2006 10:12 am 
Newbie

Joined: Fri Aug 25, 2006 4:57 am
Posts: 4
Does anyone know, if there will be a more comfortable way mapping composite keys? Creating one extra-PK-class works fine, but it is getting ugly when you have a table with a PK consisting of a bigger number of columns.
(I know 'artificial key' is the magic word here but I have to work with a legacy db-scheme...)


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