Why my @MappedSuperClass doesn't work?
-
29-05-2021 - |
Question
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 2
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 ;).
OTHER TIPS
Thanks for answering this Cytemax in your comments. I found more information for clarafication here: http://openjpa.apache.org/builds/1.0.0/apache-openjpa-1.0.0/docs/manual/manual.html#jpa_overview_pc_identitycls
The example given doesn't use annotations but you mentioned them in your comments. Kind of an odd thing for openJPA to need but as I view the table and java object hierarchy in my mind it sort of makes sense.