Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPA - Persisting a One to Many relationship

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!

like image 439
JMM Avatar asked Nov 25 '09 09:11

JMM


People also ask

How do you persist many-to-many relationships in JPA?

In JPA we use the @ManyToMany annotation to model many-to-many relationships. This type of relationship can be unidirectional or bidirectional: In a unidirectional relationship only one entity in the relationship points the other. In a bidirectional relationship both entities point to each other.

How do you create a one to many relationship in JPA?

Many-To-One relation between entities: Where one entity (column or set of columns) is/are referenced with another entity (column or set of columns) which contain unique values. In relational databases these relations are applicable by using foreign key/primary key between tables.

How do you map a one to many relationship in hibernate?

The type attribute holds the hibernate mapping type, this mapping types will convert from Java to SQL data type. The <many-to-one> element is used to set the relationship between EMPLOYEE and ADDRESS entities. The name attribute is set to the defined variable in the parent class, in our case it is address.


1 Answers

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); 
like image 59
Bobby Avatar answered Sep 17 '22 17:09

Bobby