-->
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.  [ 1 post ] 
Author Message
 Post subject: Hibernate uses multiple selects instead of one for collectio
PostPosted: Sun May 19, 2013 7:08 am 
Beginner
Beginner

Joined: Tue Nov 22, 2005 6:55 am
Posts: 41
Hi, I am using HIbernate 3, Oracle and Java. I have an Employee class and this mapping:
Code:

   <set name="meetings" table="EMPLOYEE_MEETING" inverse="false" lazy="false" fetch="select" cascade="all" >
      <key column="EMPLOYEE_ID" />
      <composite-element class="EmployeeMeeting">
         <parent name="employee" />
         <many-to-one name="meeting" column="MEETING_ID" not-null="true" cascade="all"
            class="Meeting" />
         <property name="opinion" column="OPINION" />
      </composite-element>
   </set>


There are over 100 meetings and Hibernate is using below selects over 100 of times:
Code:

    Hibernate: select this_.EMPLOYEE_ID as EMPLOYEE1_0_0_, this_.firstname as     firstname0_0_, this_.lastname as lastname0_0_ from EMPLOYEE this_ where this_.EMPLOYEE_ID=?
    Hibernate: select meetings0_.EMPLOYEE_ID as EMPLOYEE1_0_, meetings0_.MEETING_ID as         MEETING2_0_, meetings0_.OPINION as OPINION0_ from EMPLOYEE_MEETING meetings0_     where meetings0_.EMPLOYEE_ID=?



Now there is a query
Code:

        List emps = session.createCriteria(Employee.class)
        .add( Restrictions.eq("age", new Long(25)) ).list();


That query returns 10 employees with age 25 and performs over 100 selects which is visible in logs.

I tried to change fetch="select" to fetch="join" but there is the cartesian effect, the query will return plenty of the same employees.. Despite there is one query to database which is visible in log file, this is also wrong solution..

How to change it so thate Hibernate could retrieve the meetings collection using as little database selects as possible but still return 10 employees?


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

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.