3

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?

1
  • why have a separate PK class just to contain a single field?! let's complicate life ... Commented Feb 10, 2018 at 13:18

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

I kind of got the idea, but not sure how you mean really. Could you please provide a code sample?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.