2015-10-28 3 views
1

Если есть N сотрудников, он выполняет N + 1 запросов. Я хочу, чтобы он выполнил только 1 запрос.критерии спящего режима, выполняющие дополнительные запросы

@Entity 
public class Employee { 

@Id 
private int employeeId; 

private String employeeName; 

private Department department; 

private List<Dependents> dependents; 

//getter setter for employeeId and employeeName 

@ManyToOne 
@JoinColumn(name = "id_department") 
public Department getDepartment() { 
    return department; 
} 

public void setDepartment(Department department) { 
    this.department = department; 
} 

@OneToMany(mappedBy = "employee", cascade = CascadeType.ALL) 
public List<Dependents> getDependents() { 
    return dependents; 
} 

public void setDependents(List<Dependents> dependents) { 
    this.dependents = dependents; 
} 

} 

@Entity 
public class Department { 

@Id 
private int departmentId; 

private String departmentName; 

//Getter setters 
} 

@Entity 
public class Dependents { 

@Id 
private int dependentsId; 

private String dependentsName; 

private Employee employee; 

//Getter setters for dependentsId and dependentsName 

@ManyToOne 
@JoinColumn(name = "id_employee") 
public Employee getEmployee() { 
    return employee; 
} 

public void setEmployee(Employee employee) { 
    this.employee = employee; 
} 
} 

Я Использование Критерии Результат

Criteria criteria=session.createCriteria(Employee.class); 
criteria.createAlias("department","department"); 
criteria.createAlias("dependents","dependents"); 
criteria.add(Restrictions.eq("department.departmentId",depId); 
return criteria.list(); 

Это Результаты в N + 1 запросов. 1-й запрос для извлечения всех сотрудников, а затем для каждого сотрудника один запрос для получения его сведений, даже если первый запрос возвратил всю необходимую информацию.

//First Query 
select this_.id_employee as id_empl1_7_5_, 
    this_.id_department as id_depart11_7_5_, 
    this_.employee_name as tx_name3_7_5_, 
    department1_.id_department as id_depart1_30_0_, 
    department1_.department_name as tx_departm2_30_0_, 
    dependent1_.id_dependent as id_depen1_6_2_, 
    dependent1_.dependent_name as tx_depend2_6_2_, 
    dependent1_.id_employee as id_employ3_6_2_, 
from 
    employee_details this_ 
inner join 
    department department1_ 
     on this_.id_department=department1_.id_department 
inner join 
    dependents dependent1_ 
     on this_.id_employee=dependent1_.id_employee 
where 
    department1_.id_department=? 

И для каждого сотрудника-это огонь один запрос:

select 
    employe0_.id_employee as id_empl1_7_0_, 
    employe0_.id_department as id_depa11_7_0_, 
    employe0_.emloyee_name as emloye2_7_0_, 
    department1_.id_department as id_depar1_30_1_, 
    department1_.department_name as depar2_30_1_, 
    dependent1_.id_department as nu_seque1_6_2_, 
    dependent1_.department_name as is_curre2_6_2_, 
    dependent1_.id_employee as id_empl2_6_2_, 
from 
    employee_details employe0_ 
left outer join 
    department department1_ 
     on employe0_.id_department=department1_.id_department 
left outer join 
    dependents dependent1_ 
     on this_.id_employee=dependent1_.id_employee 
where 
    employe0_.id_employee=? 

Я уже пробовал FetchMode.Join, Выборочная ResultTransformer и даже установки рвение. Но никто из них не работал.

+0

Вы нашли решение, если да, любезно скажите мне, потому что я тоже застрял в этой проблеме? – Sahil

ответ

0

Вы пробовали

Criteria criteria = session.createCriteria(Employee.class); 
criteria.setFetchMode("department", FetchMode.EAGER); 
criteria.setFetchMode("dependents", FetchMode.EAGER); 
criteria.add(Restrictions.eq("department.departmentId",depId)); 

Или HQL:

session.createQuery("from Employee emp join fetch emp.department dept join fetch emp.dependents depnd where dept.departmentId = :deptid").setParameter("deptid",depId).list(); 
0

Извините за nurdy но вы уверены, что эти два SQLs производятся именно в criteria.list(); вызова? Потому что кажется, что Hibernate пытается обновить каждый экземпляр Employee. И вы правы - он не нужен, когда создается список. Может быть, после этого есть итерация, где вызывается обновление (возможно, чтобы прикрепить экземпляр к сеансу)?