JPA - 坚持一对多的关系 [英] JPA - Persisting a One to Many relationship

查看:63
本文介绍了JPA - 坚持一对多的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许这是一个愚蠢的问题,但它让我烦恼。

Maybe this is a stupid question but it's bugging me.

我有一对双向的员工与车辆的关系。当我第一次在数据库中保留一个Employee(即它没有分配的ID)时,我也希望它的相关Vehicle保持不变。

I have a bi-directional one to many relationship of Employee to Vehicles. When I persist an Employee in the database for the first time (i.e. it has no assigned ID) I also want its associated Vehicles to be persisted.

目前这对我来说很好,除了我保存的Vehicle实体没有自动映射关联的Employee,而在数据库中的employee_id外键列车辆表是空的。

This works fine for me at the moment, except that my saved Vehicle entity is not getting the associated Employee mapped automatically, and in the database the employee_id foreign key column in the Vehicle table is null.

我的问题是,是否可以在持有员工本身的同时保持车辆的员工?我意识到需要首先保存员工,然后保存车辆。 JPA可以自动为我做这个吗?或者我必须做以下事情:

My question is, is it possible to have the Vehicle's employee persisted at the same time the Employee itself is being persisted? I realise that the Employee would need to be saved first, then the Vehicle saved afterwards. Can JPA do this automatically for me? Or do I have to do something like the following:

Vehicle vehicle1 = new Vehicle();
Set<Vehicle> vehicles = new HashSet<Vehicle>();
vehicles.add(vehicle1);

Employee newEmployee = new Employee("matt");
newEmployee.setVehicles(vehicles);
Employee savedEmployee = employeeDao.persistOrMerge(newEmployee);

vehicle1.setAssociatedEmployee(savedEmployee);
vehicleDao.persistOrMerge(vehicle1);

谢谢!

编辑:按要求,这是我的映射(没有所有其他方法等。)

As requested, here's my mappings (without all the other methods etc.)

@Entity
public class Employee {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name="employee_id")
    private Long id;

    @OneToMany(mappedBy="associatedEmployee", cascade=CascadeType.ALL)
    private Set<Vehicle> vehicles;

    ...

}

@Entity 
public class Vehicle {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name="vehicle_id")
    private Long id;

    @ManyToOne
    @JoinColumn(name="employee_id")
    private Employee associatedEmployee;

    ...
}

我刚才意识到我应该在我的Employee类上定义了以下方法:

I just realised I should have had the following method defined on my Employee class:

public void addVehicle(Vehicle vehicle) {
    vehicle.setAssociatedEmployee(this);
    vehicles.add(vehicle);
}

现在上面的代码如下所示:

Now the code above will look like this:

Vehicle vehicle1 = new Vehicle();

Employee newEmployee = new Employee("matt");
newEmployee.addVehicle(vehicle1);
Employee savedEmployee = employeeDao.persistOrMerge(newEmployee);

更简单,更清洁。感谢大家的帮助!

Much simpler and cleaner. Thanks for your help everyone!

推荐答案

在持久化员工之前,您必须在Vehicle上设置associatedEmployee。

You have to set the associatedEmployee on the Vehicle before persisting the Employee.

Employee newEmployee = new Employee("matt");
vehicle1.setAssociatedEmployee(newEmployee);
vehicles.add(vehicle1);

newEmployee.setVehicles(vehicles);

Employee savedEmployee = employeeDao.persistOrMerge(newEmployee);

这篇关于JPA - 坚持一对多的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆