JPA - 保持一对多关系
也许这是一个愚蠢的问题,但它困扰着我。
我有员工与车辆之间的双向一对多关系。当我第一次在数据库中保留一个员工(即它没有分配的 ID)时,我还希望保留其关联的车辆。
目前这对我来说效果很好,除了我保存的车辆实体没有自动映射关联的员工,并且在数据库中车辆表中的employee_id外键列为空。
我的问题是,是否可以在保留员工本身的同时保留车辆的员工?我意识到需要先保存员工,然后再保存车辆。 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);
谢谢!
编辑:根据要求,这是我的映射(没有所有其他方法等)
@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 类上定义以下方法:
public void addVehicle(Vehicle vehicle) {
vehicle.setAssociatedEmployee(this);
vehicles.add(vehicle);
}
现在上面的代码将如下所示:
Vehicle vehicle1 = new Vehicle();
Employee newEmployee = new Employee("matt");
newEmployee.addVehicle(vehicle1);
Employee savedEmployee = employeeDao.persistOrMerge(newEmployee);
更加简单和干净。感谢大家的帮助!
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.
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.
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);
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:
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在保留 Employee 之前,您必须在车辆上设置关联的 Employee。
You have to set the associatedEmployee on the Vehicle before persisting the Employee.
一种方法是在关系的“一”侧设置级联选项:
这样,当您调用
它时,它也会保存车辆。
One way to do that is to set the cascade option on you "One" side of relationship:
by this, when you call
it will save the vehicles too.