为什么我的@MappedSuperClass不起作用?

时间:2012-04-04 15:44:55

标签: inheritance ejb entity openjpa mappedsuperclass

我有以下@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 Class中找不到@Id属性。

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

此致

Cytemax

2 个答案:

答案 0 :(得分:0)

感谢您在评论中回答此Cytemax。我在这里找到了更多关于clarafication的信息: http://openjpa.apache.org/builds/1.0.0/apache-openjpa-1.0.0/docs/manual/manual.html#jpa_overview_pc_identitycls

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

答案 1 :(得分:0)

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

此@IdClass声明两个属性,并且必须覆盖“等于”和“哈希码”方法。

也许这会帮助别人;)。