【问题标题】:Why my @MappedSuperClass doesn't work?为什么我的@MappedSuperClass 不起作用?
【发布时间】:2012-04-04 15:44:55
【问题描述】:

我有以下 @MappedSuperClass 和 @Entity :

@MappedSuperclass 
public class SuperClass implements Serializable {....}

@Entity
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
@Table(name = "TABLE1")
public class Table1 extends SuperClass implements Serializable {...}

@Entity
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
@Table(name = "TABLE2")
public class Table2 extends SuperClass implements Serializable {...}

在数据库中,两个表都有相同的列,所以我所有的属性都在 SuperClass 中:

@Id
private String attr1;

@Id
private String attr2;

@Column(name="DATA")
private String data;

// getters and setters

但是当我尝试使用@entity(table1 或 table2)之一执行查询时,我得到一个 OpenJPA 错误:

Error pre-processing class table1 with weblogic.deployment.PersistenceUnitInfoImpl$ClassPreProcessorImpl@205c54b'
<openjpa-1.1.0-r422266:657916 fatal user error> 
org.apache.openjpa.util.MetaDataException: Type "class SuperClass" with application identity and no superclass does not declare an id class.  
This type is not eligible for builtin identity, so it must declare an id class.

我不明白为什么在@Entity 类中找不到属性@Id。

如果有人有任何想法,请随时帮助我:)

问候,

Cytemax

【问题讨论】:

    标签: inheritance ejb entity openjpa mappedsuperclass


    【解决方案1】:

    感谢您在 cmets 中回答此 Cytemax。我在这里找到了更多澄清信息: http://openjpa.apache.org/builds/1.0.0/apache-openjpa-1.0.0/docs/manual/manual.html#jpa_overview_pc_identitycls

    给出的示例没有使用注释,但您在 cmets 中提到了它们。 openJPA 需要一种奇怪的东西,但是当我在脑海中查看表和 java 对象层次结构时,这有点道理。

    【讨论】:

      【解决方案2】:

      我的@Entity 类必须有一个@IdClass(SuperClassPK.class),因为我的主键包含两个属性(attr1 和 attr2)。

      这个@IdClass 声明了两个属性并且必须覆盖“equals”和“hashcode”方法。

      也许这会对其他人有所帮助;)。

      【讨论】:

        猜你喜欢
        • 2015-09-25
        • 2018-03-05
        • 2013-04-19
        • 2015-10-17
        • 2016-02-20
        • 2023-04-03
        • 2012-01-07
        • 2012-06-11
        • 2015-08-26
        相关资源
        最近更新 更多