Hibernate 继承:架构验证:缺少列
Hibernate inheritance: Schema-validation: missing column
我正在尝试使用策略 InheritanceType.JOINED
在 Hibernate
中实现继承。但是,在启动应用程序时失败并出现以下异常:
Caused by: org.hibernate.tool.schema.spi.SchemaManagementException: Schema-validation: missing column [EMAIL] in table [CLIENT]
我不知道它为什么要在 Client
table 中寻找字段 email
,因为实体模型指定它在抽象超类中 - User
.客户端只有特定于它的字段。
这是我的实体模型的样子。
UserTable.java
@Entity
@Table(name = "USER")
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class UserTable implements Serializable {
private static final long serialVersionUID = 1L;
@Column(name = "USER_ID", nullable = false)
private Long userId;
@EmbeddedId
private UserTablePK userTablePK;
@Column(name = "PASSWORD", nullable = false, length = 512)
private String password;
@Column(name = "FIRSTNAME", nullable = false, length = 256)
private String firstName;
public UserTable() {
}
public UserTable(Long userId, String email, String password, String firstName) {
this.userId = userId;
this.userTablePK = new UserTablePK(email);
this.password = HashCalculator.calculateSha256Hash(password, SecurityConstants.saltConstant());
this.firstName = firstName;
}
// get, set
}
UserTablePK.java
@Embeddable
public class UserTablePK implements Serializable {
@Column(name = "EMAIL", nullable = false, length = 256)
private String email;
public UserTablePK() {
}
public UserTablePK(String email) {
this.email = email;
}
ClientTable.java
@Entity
@Table(name = "CLIENT")
public class ClientTable extends UserTable implements Serializable {
@Column(name = "WEIGHT")
private String weight;
@Column(name = "HEIGHT")
private Integer height;
public ClientTable() {
}
public ClientTable(Long clientId, String weight, Integer height, String email, String password, String firstName) {
super(clientId, email, password, firstName);
this.weight = weight;
this.height = height;
}
}
同样,为什么要在子类table中寻找email
字段?我使用 Liquibase 生成架构,并检查了架构是否正确。 Client
table里没有email
,只有User
table里才是应该的。实体模型对应的是那个,有什么问题吗?
因为它是父class中的pk ....你需要从子table引用父table,因此你还需要一个'email' 子项 table 中的列以通过它返回父项 table
编辑:您只需要在 'CLIENT' table 中添加一列 "email" 以仅使用 JPA 默认配置....如果您想编辑 FK从子项到父项的引用,您需要按照 here
中的描述覆盖 @PrimaryKeyJoinColumn
我正在尝试使用策略 InheritanceType.JOINED
在 Hibernate
中实现继承。但是,在启动应用程序时失败并出现以下异常:
Caused by: org.hibernate.tool.schema.spi.SchemaManagementException: Schema-validation: missing column [EMAIL] in table [CLIENT]
我不知道它为什么要在 Client
table 中寻找字段 email
,因为实体模型指定它在抽象超类中 - User
.客户端只有特定于它的字段。
这是我的实体模型的样子。
UserTable.java
@Entity
@Table(name = "USER")
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class UserTable implements Serializable {
private static final long serialVersionUID = 1L;
@Column(name = "USER_ID", nullable = false)
private Long userId;
@EmbeddedId
private UserTablePK userTablePK;
@Column(name = "PASSWORD", nullable = false, length = 512)
private String password;
@Column(name = "FIRSTNAME", nullable = false, length = 256)
private String firstName;
public UserTable() {
}
public UserTable(Long userId, String email, String password, String firstName) {
this.userId = userId;
this.userTablePK = new UserTablePK(email);
this.password = HashCalculator.calculateSha256Hash(password, SecurityConstants.saltConstant());
this.firstName = firstName;
}
// get, set
}
UserTablePK.java
@Embeddable
public class UserTablePK implements Serializable {
@Column(name = "EMAIL", nullable = false, length = 256)
private String email;
public UserTablePK() {
}
public UserTablePK(String email) {
this.email = email;
}
ClientTable.java
@Entity
@Table(name = "CLIENT")
public class ClientTable extends UserTable implements Serializable {
@Column(name = "WEIGHT")
private String weight;
@Column(name = "HEIGHT")
private Integer height;
public ClientTable() {
}
public ClientTable(Long clientId, String weight, Integer height, String email, String password, String firstName) {
super(clientId, email, password, firstName);
this.weight = weight;
this.height = height;
}
}
同样,为什么要在子类table中寻找email
字段?我使用 Liquibase 生成架构,并检查了架构是否正确。 Client
table里没有email
,只有User
table里才是应该的。实体模型对应的是那个,有什么问题吗?
因为它是父class中的pk ....你需要从子table引用父table,因此你还需要一个'email' 子项 table 中的列以通过它返回父项 table
编辑:您只需要在 'CLIENT' table 中添加一列 "email" 以仅使用 JPA 默认配置....如果您想编辑 FK从子项到父项的引用,您需要按照 here
中的描述覆盖 @PrimaryKeyJoinColumn