5

I have an entity class which inherits from four level of inheritance in which the top level parent defines the primary key (@Id) and I'm having trouble figuring out what I did wrong as I get this error:

Entity class [class D] 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.

Here's the hierarchy:

A->B->C->(Entity)D

This is my non-entity class that gives the values to its children:

import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;

@MappedSuperclass
public class A implements Serializable {
@Id
@GeneratedValue
protected Long id;
@Temporal(javax.persistence.TemporalType.TIMESTAMP)
protected Date deleted;

 public Date getDeleted() {
    return deleted;
 }

 public void setDeleted(Date deleted) {
    this.deleted = deleted;
 }

 public Long getId() {
    return id;
 }

 public void setId(Long id) {
    this.id = id;
 }
}

and this is one of its child :

@MappedSuperclass
public abstract class B extends A implements Serializable {

}

B->C

import javax.persistence.Entity;
import javax.persistence.Transient;


@MappedSuperclass
public class C extends B{

protected String name;
protected String description;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}
}

And finally C-> (Entity)D

@Entity
public class D extends C{
private String codeD;

public String getCodeD(){return codeD;}
public String setCodeD(String codeD) {this.codeD = codeD;}
}

According to every source I've found, normally with the @MappedSuperclass and implementing Serializable it should work. Thought I've tried implementing Serializable even every step of the hierarchy but I received the same error message.

I'm currently using Payara 4.1.1.1621. I don't know if that might be the problem as I've seen this kind of error in Payara on some thread but they all miraculously resolved themselves.

  • Entities are fine. Your JPA provider appears to be the one with the problem, which one is it ? – Neil Stockton Jan 18 '17 at 06:57
  • 1
    The mapping you posted seems OK, in fact I tried to reproduce it in a simple app, and it works with Payara Server 162. Do you use any ORM XML mapping file? Is it possible that it contains additional mapping that could cause problems? – OndroMih Jan 18 '17 at 12:06
  • My JPA provider is EclipseLink and the javax.persistence version of EclipseLink is 2.1.0 And no I'm not using any ORM XML mapping file. – Antoine de Villers Jan 18 '17 at 20:31
  • Update: I've tried using the update 2.1.1 of EclipseLink's JPA. Still getting the same error message. – Antoine de Villers Jan 18 '17 at 20:48
  • @OndroMih Thank you for your suggestion. Your suggestion fixed my issue. I had an incorrect mapping in ORM.xml due to a copy and paste issue. – Mr. Port St Joe Oct 15 '21 at 21:22

2 Answers2

3

I resolved this error by adding my base class to persistence.xml.

qasic
  • 53
  • 6
1

The configuration seems to be as the specification suggests.

The only thing that comes to my mind that can be added is the @Column annotation to explicitly declare the database column names:

@Id
@GeneratedValue
@Column(name = "id")
protected Long id;
@Temporal(javax.persistence.TemporalType.TIMESTAMP)
@Column(name = "deleted")
protected Date deleted;

Try it out. If it still doesnt work try to annotate all the fields in the classes marked as @MappedSuperClass.

Also, class C should have its fields marked as protected not private?

Maciej Kowalski
  • 25,605
  • 12
  • 54
  • 63