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?
The solution is to explicitly name each column in BaseEntity, as follows: