Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eclipselink ValidationException

I am trying to adapt my data model to use a BaseEntity base-class. The following code represents the general idea:

    @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 ...]
}

The following is an example of a derived entity:

@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 ...]
}

If I'm understanding the JPA documentation correctly, this should be perfectly valid, yet I get the following error from EclipseLink when deploying my code:

Entity class [class com.x.y.z.Address] 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.

I've tried a few things to work around this:

  • Upgrading to EclipseLink 2.3 (I'm currently using 2.2)
  • Making BaseEntity non-abstract
  • Moving the @Id annotation and field directly into Address
  • Annotating the getId() method instead of field both in BaseEntity and in Address.

None of these approaches have had any effect, short of migrating my code to Hibernate (or some other, better, JPA implementation), is there anything I can do?

like image 681
Tim Avatar asked Jun 27 '26 04:06

Tim


1 Answers

The solution is to explicitly name each column in BaseEntity, as follows:

@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;
like image 101
Tim Avatar answered Jun 28 '26 18:06

Tim