1

我错过了一些非常基本的东西。给定以下两个实体Department(反向方)和(拥有方)形成从到Employee的一对多关系。DepartmentEmployee

部门.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

4

1 回答 1

3

这意味着对于您的特定示例,如果您更改代码以将员工添加到部门(而不是设置部门的其他方式),那么您会注意到这不会自动在员工上设置部门。您将不得不编写代码来明确执行此操作。

因此,即使您显示的特定代码路径确实有效,但这并不意味着您可以依赖它。我可以猜一下为什么会这样 - 集合是延迟加载的,并且由于在加载集合之前对象是持久的,它能够从数据库中提取正确的数据。

最好的解决方案是注意文档中的建议,并在双向关系的两端正确设置状态,尽管有性能考虑(稍后可以对其进行微调)。

于 2015-06-09T20:14:52.557 回答