保存双向关系的一面

时间:2015-11-18 20:04:47

标签: jpa

在双向关系中,我们可以从任何一方进行保存吗?

@Entity
public class Department {
  @OneToMany
  private Collection<employee> employees;
  ...
}


@Entity
public class Employee {
   @ManyToOne
   private Department department;
   ...
}

我们可以决定吗

departement.setEmployee(employees)
departmentDao.save(departement);

employee.setDepartment(department);
employeeDao.save(employee);

1 个答案:

答案 0 :(得分:1)

我认为这取决于您在关系中使用的级联类型(@ManyToOne,@ OneToMany等)以及您放置它的实体。

https://docs.oracle.com/javaee/6/api/javax/persistence/CascadeType.html