我有4个持久化类,它们都有相同的字段(确切地说),它们之间唯一的3个区别是1)类名,2)表名和3)数据。我知道这可能对一些人来说很奇怪,但相信我,我有一个很好的理由,我不会在这里讨论。
现在,我正在使用hibernate注释来配置我的类,它应该是这样工作的:
@Entity
@Table(name = "store")
public class Store
{
@Id
@Column(name = "unique_id")
protected String id;
@Column
protected String category;
...
}。。对于单个独立的类,这确实有效,但是有许多字段要映射,我想为所有四个类似的类一次性完成所有这些,即:
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
{}然而,当我尝试这样做时,我得到了以下异常:
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)我猜这是因为没有在超类中搜索标识符?
在这种情况下,有没有办法利用继承?
谢谢,保罗。
发布于 2010-08-18 14:39:10
@MappedSuperclass
public class StoreBase有关详细信息,请参阅docs。
发布于 2010-08-18 14:42:32
看看@MappedSuperclass吧。
https://stackoverflow.com/questions/3509481
复制相似问题