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

时间:2018-02-10 09:48:21

标签: java hibernate jpa inheritance database-schema

我正在尝试使用策略HibernateInheritanceType.JOINED中实现继承。但是,在启动应用程序时,它会失败,但例外情况为:

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

我不知道为什么它在email表中寻找字段Client,因为实体模型指定它在抽象超类中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;
    }
}

同样,为什么要在子类表中查找email字段?我使用Liquibase生成Schema,我检查了 - 架构是正确的。 email表中没有Client,它只在User表中显示。实体模型对应于那个,那么问题是什么?

1 个答案:

答案 0 :(得分:2)

因为它是父类中的pk ....您需要从子表中引用父表,因此您还需要子表中的“email”列将其引用回父表

编辑:你只需要在'CLIENT'表中添加一个“email”列,只使用JPA默认配置....如果你想编辑从子到父引用的FK你将需要覆盖@PrimaryKeyJoinColumn描述为here