Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javax.persistence annotations and inheritance

Tags:

I have 4 persistent classes which all have the same fields (exactly) the only 3 difference between them is 1) the class name, 2) the table name and 3) the data. i am aware that this might seem strange to some but trust me there is a good reason which i won't go into here.

now, i'm using hibernate annotations to configure my class which should work like so:

@Entity @Table(name = "store") public class Store {  @Id  @Column(name = "unique_id")  protected String id;  @Column  protected String category;  ... } 

.. and this does work, for a single stand-alone class, however there are many fields to map and i'd like to do it all in one hit for all four similar classes, ie:

public class StoreBase {  @Id  @Column(name = "unique_id")  protected String id;  @Column  protected String category;  ... }  @Entity @Table(name = "store1") public class Store1 extends StoreBase {}  @Entity @Table(name = "store2") public class Store2 extends StoreBase {}  @Entity @Table(name = "store3") public class Store3 extends StoreBase {}  @Entity @Table(name = "store4") public class Store4 extends StoreBase {} 

however when attempting this i get the following exception:

Caused by: org.hibernate.AnnotationException: No identifier specified for entity: package.entities.Store1  at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:672)  at org.hibernate.cfg.AnnotationConfiguration.processArtifactsOfType(AnnotationConfiguration.java:546)  at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:291)  at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1292)  at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:867) 

i'm guessing this is because the super class is not being searched for the identifier?

is there a way to utilise inheritance in this context?

thanks, paul.

like image 572
pstanton Avatar asked Aug 18 '10 06:08

pstanton


People also ask

How does JPA inheritance work?

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.

What is inheritance strategy?

The mapped superclass strategy is the simplest approach to mapping an inheritance structure to database tables. It maps each concrete class to its own table. That allows you to share the attribute definition between multiple entities. But it also has a huge drawback.

Which inheritance strategy is better in Hibernate?

Hibernate/JPA Single Table Inheritance. The single table strategy maps all entities of the inheritance structure to the same database table. This approach makes polymorphic queries very efficient and provides the best performance.


2 Answers

@MappedSuperclass public class StoreBase 

See docs for more info.

like image 58
Bozho Avatar answered Sep 28 '22 17:09

Bozho


Have a look at @MappedSuperclass.

like image 23
cyphorious Avatar answered Sep 28 '22 16:09

cyphorious