简体   繁体   中英

javax.persistence annotations and inheritance

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.

@MappedSuperclass
public class StoreBase

See docs for more info.

看看@MappedSuperclass

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM