모든 엔티티에 기본 키가 있어야 함

OpenJPA에서는 엔티티 클래스를 기본 키 없이 정의할 수 있으며 이 경우 기본이 되는 ID라는 기본 키가 생성됩니다. EclipseLink의 경우, 모든 엔티티 클래스에 @Id, @EmbeddedId 또는 @IdClass 어노테이션을 사용하는 기본 키로서 식별되는 속성이 있어야 합니다.

이 규칙은 엔티티 클래스를 스캔하여 엔티티 클래스에 @Id, @EmbeddedId 또는 @IdClass 어노테이션이 있는 속성이 포함되는지 확인합니다. 이러한 어노테이션이 있는 속성이 포함되지 않은 클래스의 경우, 이 엔티티의 OpenJPA 생성 열을 사용하는 ID 속성을 추가하십시오. 이 규칙은 하나의 클래스에 포함된 엔티티의 유효성 검증으로 제한됩니다. 엔티티 클래스가 다른 엔티티 클래스 또는 맵핑된 수퍼클래스를 확장하는 경우 이 규칙은 누락된 기본 키에 플래그를 지정하지 않습니다. 이러한 경우에 다음 예와 같은 런타임 예외가 수신되면 기본 키가 누락된 것입니다.

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.

다음 엔티티 클래스에서는 이 규칙에 의해 Entity 어노테이션에 플래그가 지정됩니다.

import javax.persistence.Entity;

@Entity
public class EntityNoId {

}

기본 키를 식별하려면 어노테이션이 있는 속성을 추가하십시오.

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;
}

OpenJPA에서 EclipseLink로의 마이그레이션 문제에 대한 정보는 OpenJPA에서 EclipseLink JPA로 마이그레이션: 맵핑 안내서를 참조하십시오.