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