Is-a和Has-a问题映射外键

时间:2019-01-18 20:03:43

标签: java mysql spring-boot jpa

在我的程序中,当我获得继承和oneToMany关系时,我意识到了一个问题。那就是:

我放手了。它将创建没有外键的表。但是当我删除继承注释时,再次运行。它创建为外国。

班级雇主

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


}

和类车辆

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


}

还有我的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

我希望继承,而在车辆表中则是雇主的外键。

实际结果是继承,但没有外键。但是当我删除继承注释时,将创建前键。

0 个答案:

没有答案