javax.persistence注释和继承

pst*_*ton 21 java annotations hibernate jpa

我有4个持久化类,它们都具有相同的字段(确切地说),它们之间唯一的区别是1)类名,2)表名和3)数据.我知道这对某些人来说可能有点奇怪,但相信我有一个很好的理由我不会进入这里.

现在,我正在使用hibernate注释来配置我的类,它应该像这样工作:

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

..这确实有效,对于一个单独的类,但是有很多字段要映射,我想在所有四个相似的类中一次性完成所有这些,即:

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
{}
Run Code Online (Sandbox Code Playgroud)

但是在尝试这个时我得到以下异常:

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)
Run Code Online (Sandbox Code Playgroud)

我猜这是因为没有搜索超类的标识符?

有没有办法在这种情况下利用继承?

谢谢,保罗.

Boz*_*zho 39

@MappedSuperclass
public class StoreBase
Run Code Online (Sandbox Code Playgroud)

有关详细信息,请参阅文档.