Is-a and Has-a Problem mapping foreign key

In my program I realize a problem when it get a inheritance and a oneToMany relationship. That is:

I put to run. It will create the tables, without the foreign key. But when I remove the Inheritance annotations, and run again. It creates as foreign.

Class Employer

package com.example.TablePerConcreteClassExample.model;

    import java.util.List;

    import javax.persistence.Column;
    import javax.persistence.DiscriminatorColumn;
    import javax.persistence.DiscriminatorValue;
    import javax.persistence.Entity;
    import javax.persistence.FetchType;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    import javax.persistence.Inheritance;
    import javax.persistence.InheritanceType;
    import javax.persistence.JoinColumn;
    import javax.persistence.ManyToOne;
    import javax.persistence.OneToMany;
    import javax.persistence.Table;

    @Entity
    @Table(name="Employer")
    @Inheritance(strategy=InheritanceType.SINGLE_TABLE)
    @DiscriminatorColumn(name="type")
    @DiscriminatorValue(value="Employer")
    public class Employer {

    @Id 
    @Column (name = "empNo", nullable=false)
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int empNo;

    @Column(name = "name", nullable=false)
    private String name;

    @OneToMany (mappedBy = "employer",
            fetch=FetchType.LAZY)
    private List<Vehicle> vehicles;

    public int getEmpNo() {
        return empNo;
    }

    public void setEmpNo(int empNo) {
        this.empNo = empNo;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<Vehicle> getVehicles() {
        return vehicles;
    }

    public void setVehicles(List<Vehicle> vehicles) {
        this.vehicles = vehicles;
    }


    }

And Class Vehicle

package com.example.TablePerConcreteClassExample.model;

    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.FetchType;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    import javax.persistence.JoinColumn;
    import javax.persistence.ManyToOne;
    import javax.persistence.Table;

    @Entity
    @Table(name="Vehicle")
    public class Vehicle {

    @Id 
    @Column (name = "id", nullable=false)
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;

    @Column(name = "name", nullable=false)
    private String name;


    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "empNo", nullable=false)
    private Employer employer;


    public int getId() {
        return id;
    }


    public void setId(int id) {
        this.id = id;
    }


    public String getName() {
        return name;
    }


    public void setName(String name) {
        this.name = name;
    }


    public Employer getEmployer() {
        return employer;
    }


    public void setEmployer(Employer employer) {
        this.employer = employer;
    }


    }

And my application.properties:

    spring.datasource.url=jdbc:mysql://localhost:3306/TablePerConcreteClass?createDatabaseIfNotExist=true&useSSL=false&useTimezone=true&serverTimezone=UTC
    spring.datasource.username=root
    spring.datasource.password=
    spring.jpa.database=MYSQL
    spring.datasource.platform=mysql
    spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
    spring.database.driverClassName=com.mysql.cj.jdbc.Driver
    spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect

    spring.jpa.hibernate.ddl-auto=create-drop

    spring.jpa.show-sql=true

    spring.servlet.multipart.enabled=true

I expected a inheritance and in the vehicle table a Employer’s foreign key.

The actual results are the inheritance, but no foreign key. But when I remove inheritance annotations, the foreing key will be created.

Inheritance is one thing and @OneToMany is a different thing.

The foreign key is on the @ManyToOne side. Post the database schema generated by Hibernate and you’ll see it.

Hello!
I don’t know why, but today it works correctly and I realize that it’s happens with just Table_Per_Class strategy. Do you know the reason?

The database schema is following below:

image

There’s no foreing key in Vehicle table.

Are you generating the database schema with Hibernate or is it a predefined schema?

I’m generating the database schema with Hibernate.

Try to replicate it with this test case template.