Hibernate - 使用mappedBy时外键为空

时间:2016-08-11 08:43:45

标签: java hibernate jpa

我有两张桌子Employee& Vehicle,一名员工可以拥有多辆车。 下面是我定义的映射:

Employee.java

@Entity(name = "emp_details")
public class Employee {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int empId;

    @OneToMany(cascade = CascadeType.ALL, mappedBy="employee")
    private List<Vehicle> vehicles = new ArrayList<>();

    public int getEmpId() {
        return empId;
    }

    public void setEmpId(int empId) {
        this.empId = empId;
    }

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

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

}

Vehicle.java

@Entity
public class Vehicle {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int vehicleId;

    @ManyToOne
    @JoinColumn(name="empId")
    private Employee employee;

    private String name;

    public Employee getEmployee() {
        return employee;
    }

    public void setEmployee(Employee employee) {
        this.employee = employee;
    }

    public int getVehicleId() {
        return vehicleId;
    }

    public void setVehicleId(int vehicleId) {
        this.vehicleId = vehicleId;
    }

    public String getName() {
        return name;
    }

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

}

主要课程

public class HibernateTest {

    public static void main(String[] args) {
        Employee emp = new Employee();

        Vehicle vehicle = new Vehicle();
        vehicle.setName("Honda");
        emp.getVehicles().add(vehicle);


        SessionFactory sFactory = new Configuration().configure().buildSessionFactory();
        Session session = sFactory.openSession();
        session.beginTransaction();
        session.save(emp);
        session.getTransaction().commit();
        session.close();

        StandardServiceRegistryBuilder.destroy(sFactory.getSessionFactoryOptions().getServiceRegistry());

    }

}

但是当我执行此操作时,Vehicle.employee_id为空。我原以为我的外键会被插入那里。

enter image description here

我错过了什么?

谢谢

4 个答案:

答案 0 :(得分:1)

如果您有双向关系,则必须这样做 设置双方的关系。

这意味着您还必须为employee设置vehicle

你可以通过调用

来做到这一点
vehicle.setEmployee(emp);

然后存储或更新您的实体(如果它尚未附加到会话)。

通常要设置双向关系,您需要在实体中提供特殊方法。

public class Vehicle {
    ...
    public void setEmployee(Employee employee) {
        this.employee = employee;
        employee.addVehicle(this)
    }
    ...
}

public class Employee {
    ...
    public void addVehicle(Vehicle v) {
          if(!vehicles.contains(v)) {
             vehicles.add(v);
          }
          if(!this.equals(v.getEmployee()) {
             v.setEmployee(this);
          }
    }
    ...
}

答案 1 :(得分:0)

我认为需要编辑你的代码。你错过了实体jpa规则。你可以搜索jpa实体。你为他读了描述。链接:enter link description here

(修改了您的Employee.java)

@Entity(name = "employee")
public class Employee {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int idx;

    @OneToMany(cascade = {CascadeType.ALL}, mappedBy = "employee")
    private List<Vehicle> vehicle = new ArrayList<>();
    ...
}

(修改了你的Vehicle.java)

@Entity
public class Vehicle {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int idx;

    @ManyToOne
    @JoinColumn(name="employee_id")
    private Employee employee;
    ...
}

答案 2 :(得分:0)

在您的Employee实体类中,您尚未在empId上使用@Column注释。在Vehicle类中,您使用@JoinColumn(name =&#34; employee_id&#34;)引用Employee,因此emp_details表中必须存在 employee_id 列。因此,您需要将Employee类修改为

     @Id
     @GeneratedValue
     @Column(name="employee_id")
     private int empId;

答案 3 :(得分:0)

您只需要从员工实体级联ontyoMany