Hi,
I have Department table consists of list of employees associated in it. When i join employee through department entity, hibernate criteria creates query which contains two departments alias part of
from clause.
Why it so? It may lead to performance issue. Any help to avoid this is much appreciated.
Thanks,
Gopi
Code:
Criteria
=====
Session session = ((HibernateEntityManagerFactory)entityManagerFactory).getSessionFactory().openSession();
Criteria criteria = session.createCriteria(Department.class, "dept");
criteria.createAlias("employees", "emp");
//criteria.setFetchMode("employees", FetchMode.SELECT);
criteria.add(Restrictions.eq("emp.empNo", 7459L));
//criteria.setResultTransformer(criteria.DISTINCT_ROOT_ENTITY);
return criteria.list();
HQL
====
select this_.DEPTNO as DEPTNO0_3_, this_.LOCATION_ID as LOCATION3_0_3_, this_.DNAME as DNAME0_3_, emp1_.EMPNO as EMPNO2_0_, emp1_.DEPTNO as DEPTNO2_0_, emp1_.ENAME as ENAME2_0_, emp1_.HIREDATE as HIREDATE2_0_, department4_.DEPTNO as DEPTNO0_1_, department4_.LOCATION_ID as LOCATION3_0_1_, department4_.DNAME as DNAME0_1_, location5_.LOCATION_ID as LOCATION1_1_2_, location5_.DESCRIPTION as DESCRIPT2_1_2_ from DEPT this_, EMP emp1_, DEPT department4_, LOCATION location5_ where this_.DEPTNO=emp1_.DEPTNO and emp1_.DEPTNO=department4_.DEPTNO(+) and department4_.LOCATION_ID=location5_.LOCATION_ID(+) and emp1_.EMPNO=?
Department Entity
public class Department {
@Id
@Column(name = "DEPTNO")
private Long deptNo;
@Column(name = "DNAME", nullable = false)
private String name;
@OneToMany(mappedBy = "department", cascade = { CascadeType.PERSIST,
CascadeType.MERGE }, fetch= FetchType.EAGER)
@Cascade( { org.hibernate.annotations.CascadeType.DELETE_ORPHAN })
//@BatchSize(size = 1)
private List<Employee> employees;
@ManyToOne
@JoinColumn(name = "LOCATION_ID", referencedColumnName = "LOCATION_ID")
private Location location;
// Getter and setter goes here
}
Employee Entity
public class Employee {
@Column(name = "ENAME")
private String empName;
@Id
@Column(name = "EMPNO", nullable = false)
@GeneratedValue(strategy=GenerationType.AUTO,generator="EMPNO_SEQ")
@SequenceGenerator(name="EMPNO_SEQ", sequenceName="EMP_SEQ")
private Long empNo;
@Column(name = "HIREDATE")
private Date hireDate;
public Date getHireDate() {
return hireDate;
}
public void setHireDate(Date hireDate) {
this.hireDate = hireDate;
}
@ManyToOne (fetch = FetchType.EAGER)
@Cascade( { CascadeType.PERSIST, CascadeType.MERGE })
@JoinColumn(name = "DEPTNO", referencedColumnName = "DEPTNO")
private Department department;
// Getter and setter goes here
}
Location Entity
public class Location {
@Id
@Column(name = "LOCATION_ID")
private Long locationId;
@Column(name = "DESCRIPTION")
private String desc;
// Getter and setter goes here
}