實現一對多關係,工作正常。JPA一對多關係查詢
我的問題是當我運行下面的查詢,如果該表有100個員工行,並且每個員工有2個部門。數據庫查詢被稱爲101次,因爲對於每個員工來說都是調用部門查詢,完成調用所有的100行花費很長時間,任何人都可以提出任何替代解決方案嗎?
請參考下面
查詢詳情它呼籲:
First query is : SELECT * FROM Employee e
Next 100 queries : SELECT * FROM DEPARTMENT d WHERE d.EmployeeId=?
JPA數據庫調用:
javax.persistence.Query query = em.createNamedQuery("SELECT * FROM Employee e", Employee.class);
return query.getResultList();
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.NamedNativeQueries;
import javax.persistence.NamedNativeQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity
@Table(name = "EMPLOYEE")
public class Employee implements Serializable
{
@Id
@Column(name = "EmployeeId")
String employeeId;
@OneToMany(mappedBy = "employee", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private List<Department> departments;
public List<Department> getDepartments() {
return departments;
}
public void setDepartments(List<Department> departments) {
this.departments = departments;
}
public String getEmployeeId() {
return employeeId;
}
public void setEmployeeId(String employeeId) {
this.employeeId = employeeId;
}
}
@Entity
@Table(name = "DEPARTMENT")
public class Department implements Serializable
{
private static final long serialVersionUID = 1L;
@Id
@Column(name = "DepartmentID")
String departmentId;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "EmployeeId", insertable = false, updatable = false)
private Employee employee;
}
輸出XML:
<Employees>
<Employee>
<name>Rob</name>
<Departments>
<Departmnet><id>1</id></Departmnet>
<Departmnet><id>2</id></Departmnet>
</Departments>
</Employee>
<Employee>
<name>Sam</name>
<Departments>
<Departmnet><id>1</id></Departmnet>
<Departmnet><id>2</id></Departmnet>
</Departments>
</Employee>
</Employees>
你爲什麼不使用聯接 –
@PhilippSander:你可能指的是抓取連接?畢竟這是JPA,持久性提供者會爲你加入。 – Gimby