代码之间有什么区别?
@LazyCollection(LazyCollectionOption.FALSE)
@OneToMany(mappedBy = "siteesTypeSite", cascade = CascadeType.ALL,fetch = FetchType.LAZY)
public Set<Sites> getSitees() {
return sitees;
}
和
@OneToMany(mappedBy = "siteesTypeSite", cascade = CascadeType.ALL,fetch = FetchType.EAGER)
public Set<Sites> getSitees() {
return sitees;
}
至于我,他们两个都有类似的结果,但第二种情况更清洁。如果我错误地纠正了我。
答案 0 :(得分:4)
注释之间的主要区别在于@OneToMany
是纯JPA注释。而@LazyCollection
是特定于Hibernate的。
因此,如果您希望代码可以在各种JPA提供程序中移植,则应使用JPA注释。
要在这两个注释之间进行说明,请考虑OneToMany
Department -> Employee
关系
案例1:
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true)
@JoinColumn(name = "DEPARTMENT_ID")
private List<Employee> employees = new ArrayList<>();
如果您使用以下命令从数据库中获取Department
对象:
entityManager.find(Department.class, 1L);
以下查询被触发以获取数据
SELECT department0_.DEPARTMENT_ID AS DEPARTMENT_ID1_0_0_,
department0_.DEPARTMENT_NAME AS DEPARTMENT_NAME2_0_0_,
department0_.LOCATION AS LOCATION3_0_0_,
employees1_.DEPARTMENT_ID AS DEPARTMENT_ID3_1_1_,
employees1_.EMPLOYEE_ID AS EMPLOYEE_ID1_1_1_,
employees1_.EMPLOYEE_ID AS EMPLOYEE_ID1_1_2_,
employees1_.DEPARTMENT_ID AS DEPARTMENT_ID3_1_2_,
employees1_.EMPLOYEE_NAME AS EMPLOYEE_NAME2_1_2_
FROM DEPARTMENT department0_
LEFT OUTER JOIN EMPLOYEE employees1_
ON department0_.DEPARTMENT_ID =employees1_.DEPARTMENT_ID
WHERE department0_.DEPARTMENT_ID=?
所以这意味着它将同时获取单个查询中的所有数据。
案例2:
@LazyCollection(LazyCollectionOption.FALSE)
@OneToMany(cascade = CascadeType.ALL,fetch = FetchType.LAZY, orphanRemoval = true)
@JoinColumn(name = "DEPARTMENT_ID")
private List<Employee> employees = new ArrayList<>();
如果使用以下方法从数据库中获取Department对象,则类似
entityManager.find(Department.class, 1L);
以下查询被触发以获取数据:
SELECT department0_.DEPARTMENT_ID AS DEPARTMENT_ID1_0_0_,
department0_.DEPARTMENT_NAME AS DEPARTMENT_NAME2_0_0_,
department0_.LOCATION AS LOCATION3_0_0_
FROM DEPARTMENT department0_
WHERE department0_.DEPARTMENT_ID=?
SELECT employees0_.DEPARTMENT_ID AS DEPARTMENT_ID3_1_0_,
employees0_.EMPLOYEE_ID AS EMPLOYEE_ID1_1_0_,
employees0_.EMPLOYEE_ID AS EMPLOYEE_ID1_1_1_,
employees0_.DEPARTMENT_ID AS DEPARTMENT_ID3_1_1_,
employees0_.EMPLOYEE_NAME AS EMPLOYEE_NAME2_1_1_
FROM EMPLOYEE employees0_
WHERE employees0_.DEPARTMENT_ID=?
总而言之,在第一种情况下,FetchType
EAGER
Employees
在Department
个查询中与JOIN
一起热切地提取。
和
在第二种情况下,使用Employees
获取Department
,但由于FetchType
为LAZY
,因此会触发单独的查询以获取Employees
。如果您移除@LazyCollection(LazyCollectionOption.FALSE)
Employees
,则在访问Employees
个实例上的Department
之前,根本不会提取{。}}。