I want to have a superclass that is common to all document types:
@Entity
public abstract class Doc implements Serializable
{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
protected long docId;
public long getDocId()
{
return docId;
}
public void setDocId(long docId)
{
this.docId = docId;
}
}
And I want to have child classes for each doc type:
@Entity
@Table(name = "DocTypeA")
public class DocTypeA extends Doc implements Serializable
{
// other child fields
}
But it gives an error and says that DocTypeA needs a primary key. How can I isolate the primary key and put it in the super class? Because all the subclasses will have that same id field.
I am using EclipseLink.
And my other question is: Why do I need to put @Entity
in the abstract class? Being an abstract class it cannot be instantiated, so what is the point of marking it as an Entity? Is it really necessary? I will not persist the superclass. I need it only for isolating the code common in all subclasses.
The stack trace is a long one, relevant part is pasted below:
Exception Description: Entity class [class repository.DocTypeA] has no primary key specified. It should define either an @Id, @EmbeddedId or an @IdClass. If you have defined PK using any of these annotations then make sure that you do not have mixed access-type (both fields and properties annotated) in your entity class hierarchy.