JPA - Persisting a One to Many relationship

后端 未结 2 693
無奈伤痛
無奈伤痛 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:55

    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 = new HashSet();
    
       //
    }
    

    by this, when you call

    Employee savedEmployee = employeeDao.persistOrMerge(newEmployee);
    

    it will save the vehicles too.

提交回复
热议问题