-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Criteria creates multiple time table joins
PostPosted: Mon Oct 18, 2010 12:45 pm 
Newbie

Joined: Mon Oct 18, 2010 12:30 pm
Posts: 3
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
}



Top
 Profile  
 
 Post subject: Re: Criteria creates multiple time table joins
PostPosted: Tue Oct 19, 2010 1:29 am 
Newbie

Joined: Mon Oct 18, 2010 12:30 pm
Posts: 3
Please advice me


Top
 Profile  
 
 Post subject: Re: Criteria creates multiple time table joins
PostPosted: Tue Oct 19, 2010 12:13 pm 
Newbie

Joined: Mon Oct 18, 2010 12:30 pm
Posts: 3
I am struck. Please help.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.