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.
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.
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.
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.
@MappedSuperclass public class StoreBase
See docs for more info.
Have a look at @MappedSuperclass.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With