Eclipselink ValidationException

时间:2011-07-25 18:54:29

标签: java inheritance jpa eclipselink

我正在尝试调整我的数据模型以使用BaseEntity基类。以下代码代表了一般概念:

    @MappedSuperclass
    public abstract class BaseEntity implements HasAuditInfo {
        @Id
        @GeneratedValue
        private Long id;
        @Column(unique = true, nullable = false)
        private String uuid;
        private Long createdById;
        @Temporal(value = TemporalType.TIMESTAMP)
        @Column(nullable = false)
        private Date createdOn;
        private Long changedById;
        @Temporal(value = TemporalType.TIMESTAMP)
        @Column(nullable = false)
        private Date changedOn;
        @Column(nullable = false)
        private Long changedOnValue;
        private Boolean active;
        private Long deactivatedById;
        @Temporal(value = TemporalType.TIMESTAMP)
        private Date deactivatedOn;
        @NotNull
        @DecimalMin("0")
        private Integer version = 0;
        private Long domainId;

        [... Getters/Setters etc ...]
}

以下是派生实体的示例:

@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class Address extends BaseEntity implements Serializable, Comparable<Address> {
    private static final long serialVersionUID = 6301090657382674578L;
    // Address Fields
    @NotBlank(message = "Address Line 1 is a mandatory field.")
    private String addressLine1;
    private String addressLine2;
    private String country;
    @NotBlank(message = "Region is a mandatory field.")
    private String region;
    @NotBlank(message = "City is a mandatory field.")
    private String city;
    @NotBlank(message = "Zipcode is a mandatory field.")
    private String zipcode;

    [... Getters/Setters etc ...]
}

如果我正确理解JPA文档,这应该是完全有效的,但在部署我的代码时,我从EclipseLink收到以下错误:

  

实体类[class   com.x.y.z.Address]没有指定主键。   它应该定义@ Id,@ EmbeddedId或@IdClass。如果你   已经使用任何这些注释定义了PK,然后确保你   没有混合访问类型(注释的字段和属性)   在您的实体类层次结构中。

我尝试了一些方法来解决这个问题:

  • 升级到EclipseLink 2.3(我目前正在使用2.2)
  • 使BaseEntity非抽象
  • 将@Id注释和字段直接移动到地址
  • 在BaseEntity和Address中注释getId()方法而不是字段。

这些方法都没有产生任何影响,没有将我的代码迁移到Hibernate(或其他一些更好的JPA实现),我能做些什么吗?

2 个答案:

答案 0 :(得分:1)

解决方案是在BaseEntity中明确命名每一列,如下所示:

@MappedSuperclass
public abstract class BaseEntity {
       @Id
       @GeneratedValue
       @Column(name = "id")
       @SearchableId
       protected Long id;
       @Column(name = "uuid", unique = true, nullable = false)
       protected String uuid;
       @Column(name = "createdById")
       protected Long createdById;
       @Temporal(value = TemporalType.TIMESTAMP)
       @Column(name = "createdOn", nullable = false)
       protected Date createdOn;
       @Column(name = "changedById")
       protected Long changedById;
       @Temporal(value = TemporalType.TIMESTAMP)
       @Column(name = "changedOn", nullable = false)
       protected Date changedOn;
       @Column(name = "changedOnValue", nullable = false)
       protected Long changedOnValue;
       @Column(name = "active")
       protected Boolean active;
       @Column(name = "deactivatedById")
       protected Long deactivatedById;
       @Temporal(value = TemporalType.TIMESTAMP)
       @Column(name = "deactivatedOn")
       protected Date deactivatedOn;
       @Version
       @Column(name = "version")
       protected Integer version = 0;
       @Column(name = "domainId")
       protected Long domainId;

答案 1 :(得分:0)

你不应该得到这个错误。如果你将@Id移动到地址中,绝对不会。

您确定在进行更改后重新编译/部署了代码吗?

如果删除TABLE_PER_CLASS继承,它是否有效? (Address有子类吗?)