JPA - Persisting a One to Many relationship

后端 未结 2 692
無奈伤痛
無奈伤痛 2020-11-30 00:23

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 dat

2条回答
  •  被撕碎了的回忆
    2020-11-30 00:51

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

提交回复
热议问题