java - 覆盖基类中的非标识符属性并使其成为子类中的标识符
问题描述
我有问题。尝试从父类继承未定义为标识符的属性时,但在我的子类中是,出现以下错误:
MappingException:您不能覆盖 [com.test.BaseEntity] 基类或 @MappedSuperclass 中的 [testDate] 非标识符属性,并使其成为 [com.test.ChildEntity] 子类中的标识符!
我的父类:
import java.sql.Timestamp;
import java.time.Clock;
import java.time.Instant;
import javax.persistence.Column;
import javax.persistence.MappedSuperclass;
import javax.persistence.PrePersist;
@MappedSuperclass
public abstract class BaseEntity {
@Column(name = "CREATION_DATE_UTC")
protected Timestamp testDate;
public Timestamp getTestDate() {
return testDate;
}
public void setTestDate(Timestamp testDate) {
this.testDate = testDate;
}
@PrePersist
public void prePersist() {
testDate = Timestamp.from(Instant.now(Clock.systemUTC()));
}
}
我的孩子班:
import java.io.Serializable;
import java.sql.Timestamp;
import javax.persistence.AttributeOverride;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.validation.constraints.NotNull;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.gson.Gson;
@Entity(name = "CHILD")
@IdClass(ChildEntityPk.class)
public class ChildEntity extends BaseEntity implements Serializable {
private static final long serialVersionUID = 4392848588125748902L;
@Id
@Column(name = "CHILD_ID", insertable = false, updatable = false)
@JsonIgnore
private Integer id;
@Id
@Column(name = "CHILD_TEST_DATE", insertable = false, updatable = false)
@JsonIgnore
private Timestamp testDate;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Timestamp getTestDate() {
return testDate;
}
public void setTestDate(Timestamp testDate) {
this.testDate = testDate;
}
@Override
public String toString() {
return new Gson().toJson(this);
}
}
解决方案
推荐阅读
- javascript - 在随机位置用 0 填充数组
- reactjs - 使用反应钩子
- webpack - webpack-cli 和 webpack-command 一起使用
- javascript - 反应组件渲染两次?
- python - 将 for 循环从 MATLAB 转换为 Python
- javascript - 如何处理这个 JSON 响应?(JS)
- python - Python 中的 Google Cloud 授权 HTTP 请求
- python - 有没有办法使用 python 将 mp3/音频文件输入到麦克风输入?
- python - Python 线程 - 程序在执行后立即结束
- c++ - 从 C++ Windows 编译 C