I have a table that has no primary key, and one cannot be created either. I can construct a unique key using three columns of this table. Now hibernate demands an id for every annotated class, how do i satisfy this id with the unique Id I can create.
Asked
Active
Viewed 8,240 times
1 Answers
1
If it is an entity type then you should use a composite key. This can be done by moving primary key fields to separate class and mapping that in entity class with an @Id annotation.
See Mapping composite primary keys and foreign keys to composite primary keys
If it is not an entity but a value type you should map it accordingly. See https://stackoverflow.com/a/1696146/324900 and Entity and value types in hibernate
-
yes you can. But you said you don't have a primary key then this table is not an 'entity' theoritically. This should be part of another entity, you can map multiple tables to single entity in Hibernate. – Reddy Jun 28 '12 at 16:21
-
1Hibernate (nor JPA in general) does not care if you physically have a primary key defined on the table. You just need a column or group of columns that uniquely identify each row. The columns must all be non-nullable. – Steve Ebersole Jun 28 '12 at 20:37
-
@Steve, seems I misunderstood the question updated my answer to put composite id as first option. – Reddy Jun 29 '12 at 06:33