簡體   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