Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate @Id via inheritance

I'm trying to duplicate something you can do in .Net but not having much luck.

Is the following not possible in Java or am I just missing something? When I run it I get told there is no identifier specified for entity Group.

public abstract class RCEntity
{
   @Id @GeneratedValue
   private int id;

   //getters & setters
}

@Entity 
public class Group extends RCEntity {
}
like image 201
Shane Courtrille Avatar asked Nov 03 '10 16:11

Shane Courtrille


People also ask

How inheritance is implemented in Hibernate?

@Inheritance - It is used to define the type of inheritance used in hibernate and it is defined in the parent class. If the Inheritance annotation is not specified or if no inheritance type is specified for an entity class hierarchy, the SINGLE_TABLE mapping strategy is used.

How many strategies are there in Hibernate inheritance?

The three strategies. Hibernate supports the three basic inheritance mapping strategies: table per class hierarchy.

What is JPA inheritance?

JPA Inheritence Overview Inheritence is a key feature of object-oriented programming language in which a child class can acquire the properties of its parent class. This feature enhances reusability of the code. The relational database doesn't support the mechanism of inheritance.


2 Answers

Add the annotation @MappedSuperclass to your super class, i.e.

@MappedSuperclass
public abstract class RCEntity
{
   @Id @GeneratedValue
   private int id;

   //getters & setters
}
like image 128
William Avatar answered Oct 07 '22 23:10

William


From this section in the docs:

Any class in the hierarchy non annotated with @MappedSuperclass nor @Entity will be ignored.

like image 37
oksayt Avatar answered Oct 07 '22 23:10

oksayt