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?