【问题标题】:javax.persistence annotations and inheritancejavax.persistence 注解和继承
【发布时间】:2011-03-31 09:12:41
【问题描述】:

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

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

@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)

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

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

谢谢,保罗。

【问题讨论】:

  • 更改了标签。这不是关于休眠注释,而是关于使用休眠的 jpa

标签: java hibernate jpa annotations


【解决方案1】:

【讨论】:

    【解决方案2】:
    @MappedSuperclass
    public class StoreBase
    

    请参阅docs 了解更多信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-02
      • 2015-08-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多