Search code examples
hibernateinheritanceentityuniqueidentifieridentifier

Can inherited entities have their own unique identifier?


Well a simple question: can the subclasses that inherit the parent class, not only have the ID of the parent class, but also have their own ID?

Like for instance we have an entity class User and the subclass Patient which extends it. The User has idUser and can the patient have not only idUser but also idPatient as its own unique identifier? Also is it any useful to have another identifier?

Hmm, maybe I should mention that it's all in the context of mapping these entities with hibernate.


Solution

  • No.

    Hibernate uses the @Id field for identity, and there can be only one of those as far as hibernate is concerned.

    You could look at using a composite id field via the @EmbeddedId annotation, which allows a custom class with multiple "id" fields, to be the key.