Search code examples
sqlhibernatehibernate-annotations

Hibernate Annotations Id for table with no primary key


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.


Solution

  • 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