我错过了一些非常基本的东西。给定以下两个实体Department
(反向方)和(拥有方)形成从到Employee
的一对多关系。Department
Employee
部门.java
@Entity
@Table(catalog = "testdb", schema = "", uniqueConstraints = {
@UniqueConstraint(columnNames = {"department_id"})})
public class Department implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "department_id", nullable = false)
private Long departmentId;
@Column(name = "department_name", length = 255)
private String departmentName;
@Column(length = 255)
private String location;
@OneToMany(mappedBy = "department", fetch = FetchType.LAZY)
private List<Employee> employeeList = new ArrayList<Employee>(0);
private static final long serialVersionUID = 1L;
// Constructors + getters + setters + hashcode() + equals() + toString().
// No defensive link (relationship) management methods have yet been added to.
// CascadeType is also kept at a distance for now.
}
雇员.java
@Entity
@Table(catalog = "testdb", schema = "", uniqueConstraints = {
@UniqueConstraint(columnNames = {"employee_id"})})
public class Employee implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "employee_id", nullable = false)
private Long employeeId;
@Column(name = "employee_name", length = 255)
private String employeeName;
@JoinColumn(name = "department_id", referencedColumnName = "department_id")
@ManyToOne(fetch = FetchType.LAZY)
private Department department;
private static final long serialVersionUID = 1L;
// Constructors + getters + setters + hashcode() + equals() + toString().
// No defensive link (relationship) management methods have yet been added to.
// CascadeType is also kept at a distance for now.
}
下面给出了无状态 EJB(使用 CMT)中的一些方法,它们分别执行持久化、合并和删除操作。
public List<Employee> persist() {
Employee employee = new Employee();
employee.setEmployeeName("a");
employee.setDepartment(entityManager.getReference(Department.class, 1L));
entityManager.persist(employee);
return employee.getDepartment().getEmployeeList();
}
public List<Employee> merge(Employee employee) {
employee.setEmployeeName("b");
employee.setDepartment(entityManager.getReference(Department.class, 1L));
return entityManager.merge(employee).getDepartment().getEmployeeList();
}
public List<Employee> remove(Employee employee) {
entityManager.remove(entityManager.contains(employee) ? employee : entityManager.merge(employee));
return entityManager.getReference(Employee.class, employee.getEmployeeId()).getDepartment().getEmployeeList();
}
public Employee getEmployeeById(Long id) {
return entityManager.find(Employee.class, id);
}
这些方法由关联的应用程序客户端在非事务环境中依次调用(一个接一个)。
List<Employee> persistedList = employeeSessionBean.persist();
for (Employee employee : persistedList) {
System.out.println(employee.getEmployeeId() + " : " + employee.getEmployeeName());
}
List<Employee> mergedList = employeeSessionBean.merge(employeeSessionBean.getEmployeeById(23L));
for (Employee employee : mergedList) {
System.out.println(employee.getEmployeeId() + " : " + employee.getEmployeeName());
}
List<Employee> listAfterRemoving = employeeSessionBean.remove(employeeSessionBean.getEmployeeById(23L));
for (Employee employee : listAfterRemoving) {
System.out.println(employee.getEmployeeId() + " : " + employee.getEmployeeName());
}
List<Employee>
关系反面的列表 ( ) 自动反映上述每个操作期间的正确状态。
- 当一个
Employee
实体被持久化时,它会在反面的列表中列出(我没有明确地将新持久化Employee
的实体添加到List<Employee>
反面)。 - 合并实体时
Employee
,对实体所做的更改会自动反映在List<Employee>
反面的相应实体中(我没有明确更改反面的员工列表(List<Employee>
)持有的相应实体)。 - 同样,当一个
Employee
实体被删除时,它也会从关系反向的列表中删除(我没有明确地从反向列表中删除该实体)。
我目前在 EcliseLink 2.6.0 上。为什么我会看到与以下文本不匹配的这种行为?
与所有双向关系一样,维护双向关系是您的对象模型和应用程序的责任。JPA 中没有魔法,如果您在集合的一侧添加或删除,您还必须从另一侧添加或删除,请参阅对象损坏。从技术上讲,如果您仅从关系的拥有方添加/删除,数据库将正确更新,但是您的对象模型将不同步,这可能会导致问题。
http://en.wikibooks.org/wiki/Java_Persistence/ManyToMany#Bi-directional_Many_to_Many