Hibernate继承:模式验证:缺少列

3

我正在尝试使用策略 InheritanceType.JOINEDHibernate 中实现继承。然而,当启动应用程序时,出现了异常:

Caused by: org.hibernate.tool.schema.spi.SchemaManagementException: Schema-validation: missing column [EMAIL] in table [CLIENT]

我不知道为什么它在Client表中查找email字段,因为实体模型指定它在抽象的超类-User中。Client仅具有特定于其自身的字段。
以下是我的实体模型样式。 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;
    }
}

为什么它在子类表中查找email字段呢?我使用Liquibase进行模式生成,并检查了——模式是正确的。在Client表中没有email,只有在User表中才有,这是应该的。实体模型与此相对应,所以问题出在哪里呢?


为什么要单独创建一个只包含一个字段的PK类?让我们把生活搞得更复杂吧... - user8558216
1个回答

2

因为它是父类中的主键。。。您需要从子表引用父表,因此您还需要在子表中添加一个'email'列,以便通过它返回到父表

编辑: 您只需要在'CLIENT'表中添加一个"email"列即可使用JPA默认配置.... 如果您想要编辑从子表到父表的外键引用,则需要覆盖@PrimaryKeyJoinColumn,如此处所述


我有点明白你的意思,但不确定你具体是什么意思。你能提供一份代码示例吗? - wesleyy

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接