繁体   English   中英

javax.persistence注释和继承

[英]javax.persistence annotations and inheritance

我有4个持久化类,它们都具有相同的字段(确切地说),它们之间唯一的区别是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)

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

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

谢谢,保罗。

@MappedSuperclass
public class StoreBase

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

看看@MappedSuperclass

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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