Home Hibernate inheritance: Schema-validation: missing column
Reply: 1

Hibernate inheritance: Schema-validation: missing column

wesleyy
1#
wesleyy Published in 2018-02-10 09:48:20Z

I am trying to implement inheritance in Hibernate with strategy 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 has only fields specific for it.

Here is how my entity model looks.

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;
    }
}

Again, why is it looking for email field in the subclass table? I used Liquibase for Schema generation, and I checked - the schema is correct. There is no email in the Client table, it's only in the User table as it should be. The entity model corresponds to that, so what's the issue?

osama yaccoub
2#
osama yaccoub Reply to 2018-02-11 10:35:29Z

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

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

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.305103 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO