All entities must have a primary key

In OpenJPA, entity classes can be defined without a primary key, in which case a default primary key called ID is generated. EclipseLink requires that all entity classes have an attribute that is identified as the primary key using the @Id, @EmbeddedId, or @IdClass annotation.

This rule scans entity classes and verifies they have an attribute annotated with @Id, @EmbeddedId, or @IdClass. For classes without these annotated attributes, add an ID attribute that uses the OpenJPA-generated column for this entity. This rule is restricted to validating entities that are contained in one class. If the entity class extends other entity classes or mapped superclasses, this rule does not flag missing primary keys. In these cases, if you get a runtime exception such as the following example, you are missing a primary key:

Exception Description: Entity class [class org.apache.openjpa.persistence.annotations.EntityA] 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.

In the following entity class, the Entity annotation is flagged by the rule.

import javax.persistence.Entity;

@Entity
public class EntityNoId {

}

To identify the primary key, add the annotated attribute.

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.TableGenerator;

@Entity
public class EntityNoId {

    @Id
    @TableGenerator(name = "OPENJPA_SEQUENCE_TABLE", table = "OPENJPA_SEQUENCE_TABLE", pkColumnName = "ID", valueColumnName = "SEQUENCE_VALUE", pkColumnValue = "0")
    @GeneratedValue(strategy = GenerationType.TABLE, generator = "OPENJPA_SEQUENCE_TABLE")
    private int id;
}

For information about OpenJPA to EclipseLink migration issues, see the OpenJPA to EclipseLink JPA Migration: Mappings guide.