Search code examples
inheritanceejbentityopenjpamappedsuperclass

Why my @MappedSuperClass doesn't work?


I have the following @MappedSuperClass and @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 {...}

In the database, both table have the same columns so all my attribute are in the SuperClass :

@Id
private String attr1;

@Id
private String attr2;

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

// getters and setters

But when I try to execute a query with one of the @entity (table1 or table2), I get an OpenJPA ERROR :

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.

I don't understand why the attribute @Id are not found in the @Entity Class.

If anyone have any ideas, feel free to help me :)

Regards,

Cytemax


Solution

  • My @Entity class must have an @IdClass(SuperClassPK.class), because my primary key contains two properties (attr1 & attr2).

    This @IdClass declare the two properties and must override the "equals" and "hashcode" method.

    Maybe this will help someone else ;).