gpt4 book ai didi

java - Eclipselink 验证异常

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:16:12 26 4
gpt4 key购买 nike

我正在尝试调整我的数据模型以使用 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 收到以下错误:

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.

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

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

除了将我的代码迁移到 Hibernate(或其他一些更好的 JPA 实现)之外,这些方法都没有任何效果,我能做些什么吗?

最佳答案

解决方法是显式命名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;

关于java - Eclipselink 验证异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6820898/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com