Java JPA - 保持一对多的关系

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1795649/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 22:44:20  来源:igfitidea点击:

JPA - Persisting a One to Many relationship

javajpaormone-to-many

提问by JMM

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

也许这是一个愚蠢的问题,但它困扰着我。

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.

我有员工与车辆的双向一对多关系。当我第一次将 Employee 持久化到数据库中时(即它没有分配的 ID),我也希望其关联的 Vehicles 被持久化。

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.

目前这对我来说很好用,除了我保存的 Vehicle 实体没有自动获取关联的 Employee 映射,并且在数据库中,Vehicle 表中的 employee_id 外键列为空。

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:

我的问题是,是否有可能在坚持员工本身的同时坚持车辆的员工?我意识到需要先保存 Employee ,然后再保存 Vehicle 。JPA 可以为我自动执行此操作吗?或者我是否必须执行以下操作:

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

Thanks!

谢谢!

Edit: 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;

    ...
}

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

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

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!

更简单,更干净。谢谢大家的帮助!

采纳答案by Bobby

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

回答by user218435

One way to do that is to set the cascade option on you "One" side of relationship:

一种方法是在关系的“一方”上设置级联选项:

class Employee {
   // 

   @OneToMany(cascade = {CascadeType.PERSIST})
   private Set<Vehicles> vehicles = new HashSet<Vehicles>();

   //
}

by this, when you call

通过这个,当你打电话

Employee savedEmployee = employeeDao.persistOrMerge(newEmployee);

it will save the vehicles too.

它也将节省车辆。