簡體   English   中英

Hibernate應用程序中的OneToMany關聯集合為null

[英]OneToMany association collection is null in Hibernate application

我正在開發一個簡單的Hibernate應用程序來測試OneToMany關聯。 我使用的實體是EmployeeDepartment ,其中有很多Employees

@Entity
public class Department {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long departmentId;

    @OneToMany(cascade=CascadeType.ALL, fetch = FetchType.EAGER, mappedBy="department")
    private Set<Employee> employees;

...
getters/setters

}

@Entity
public class Employee {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long employeeId;

    @ManyToOne
    @JoinColumn(name="employee_fk")
    private Department department;

...
getters/setters

}

我用以下方式創建一些記錄:

tx.begin();

        Department department = new Department();
        department.setDepartmentName("Sales");
        session.persist(department);

        Employee emp1 = new Employee("Ar", "Mu", "111");
        Employee emp2 = new Employee("Tony", "Almeida", "222");
        Employee emp3 = new Employee("Va", "Ka", "333");

        emp1.setDepartment(department);
        emp2.setDepartment(department);
        emp3.setDepartment(department);


        session.persist(emp1);
        session.persist(emp2);
        session.persist(emp3);

        Set<Employee> emps = department.getEmployees();
        emps.remove(emp2);

但是在最后一行: emps.remove(emp2); 我收到NullPointerExceptionemps集合為null 我試圖用以下方式更改協會的所有者:

 @Entity
    public class Department {

        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private Long departmentId;

        @OneToMany(cascade=CascadeType.ALL, fetch = FetchType.EAGER)
        @JoinColumn(name="department_fk")
        private Set<Employee> employees;

    ...
    getters/setters

    }

    @Entity
    public class Employee {

        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private Long employeeId;

        @ManyToOne
        @JoinColumn(name="department_fk", insertable=false, updatable=false)
        private Department department;

    ...
    getters/setters

    }

但是結果相同。 為什么未創建Employees Set 為了使其起作用必須進行哪些更改?

讓我們仔細看看您的代碼:

Department department = new Department();
department.setDepartmentName("Sales");
session.persist(department);

Employee emp1 = new Employee("Ar", "Mu", "111");
Employee emp2 = new Employee("Tony", "Almeida", "222");
Employee emp3 = new Employee("Va", "Ka", "333");

emp1.setDepartment(department);
emp2.setDepartment(department);
emp3.setDepartment(department);


session.persist(emp1);
session.persist(emp2);
session.persist(emp3);

Set<Employee> emps = department.getEmployees();

您已經創建了Department ,三個Employees ,並為每個Employee設置了對該Department引用並將其持久化。

Set emps將為null,因為在將員工emps到數據庫之后,您沒有重新加載department 如果您這樣更改代碼:

....
session.persist(emp3);
department = session.get(Department.class, department.getDepartmentId());

Set<Employee> emps = department.getEmployees();
emps.remove(emp2);

你應該沒事的。

我想還有一點要提-之間的關系DepartmentEmployeesmappedBy Department ,其原意是指Department是讓我們說“主人”實體和它的Employee是“奴隸”,或者,如果你願意, Department承擔全部責任維護映射和參考。 但是,在我上面評論的代碼中,您將這種映射責任放在Employee實體上。 您可能希望以這種方式更改代碼:

department.add(emp1);
department.add(emp2);
department.add(emp2);

session.persist(department);

為了使Department做好維護映射和參考的工作。

您自己可以通過構造函數或通過為字段指定初始值來初始化集合。

實際上,當您從數據庫中獲取對象時,Hibernate會將集合初始化為一個空集合。 但是,當您自己創建並保留對象時,Hibernate不會真正觸摸您創建的對象,而僅保留它,因此在這種情況下它不會為您初始化字段。

如果您想確保該字段永遠不會為空(這是我所希望的),那么即使在保留之前,也要自己確保該字段為空。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM