[英]Hibernate inheritance: Schema-validation: missing column
I am trying to implement inheritance in Hibernate
with strategy InheritanceType.JOINED
. 我正在尝试使用策略
InheritanceType.JOINED
在Hibernate
实现InheritanceType.JOINED
。 However, when starting up the application it fails with the exception: 但是,在启动应用程序时,它会失败并出现以下异常:
Caused by: org.hibernate.tool.schema.spi.SchemaManagementException: Schema-validation: missing column [EMAIL] in table [CLIENT]
I don't know why it is looking for the field email
in the Client
table, since the entity model specifies it is in the abstract superclass - User
. 我不知道为什么它要在
Client
表中查找字段email
,因为实体模型指定它在抽象的超类User
。 Client has only fields specific for it. 客户只有特定的字段。
Here is how my entity model looks. 这是我的实体模型的外观。
UserTable.java 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 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 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;
}
}
Again, why is it looking for email
field in the subclass table? 同样,为什么要在子类表中查找
email
字段? I used Liquibase for Schema generation, and I checked - the schema is correct. 我使用Liquibase进行模式生成,并检查了-模式是否正确。 There is no
email
in the Client
table, it's only in the User
table as it should be. “
Client
表中没有email
,它只是应该在“ User
表中。 The entity model corresponds to that, so what's the issue? 实体模型与此相对应,那么问题是什么?
Because it's the pk in the parent class .... You need to refer to the parent table from the child table, hence you also need an 'email' column in the child table to refer through it back to the parent table 因为它是父类中的pk...。您需要从子表中引用父表,因此,您还需要在子表中有一个“ email”列,以将其通过引用返回到父表
EDIT : You will just need to add a column "email" in 'CLIENT' table to just use the JPA default config .... if you want to edit the FK that references from child to parent you will need to override @PrimaryKeyJoinColumn as described here 编辑:您只需要在“客户”表中添加一列“电子邮件”即可使用JPA默认配置....如果要编辑从子对象到父对象的FK,则需要覆盖@PrimaryKeyJoinColumn作为在这里描述
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.