I am using hibernate version: 2.1.3 Oracle 9i
I have three classes/tables. Order, LineItem and Item. Order has 1->M with LineItem and LineItem has M->1 with Item. I would like to use Criteria interface to
early load all the 3 tables from Order Side. The code is as follows:
Code:
Criteria c = session.createCriteria(Order.class)
.setFetchMode("lineItem", FetchMode.EAGER);
c.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
c.createCriteria("lineItem").setFetchMode("item", FetchMode.EAGER);
Iterator iterators = c.list().iterator();
while (iterators.hasNext()) {
Order order = (Order) iterators.next();
System.out.println("order.getEmployeeName() = " + order.getEmployeeName());
Set lineItemSet = order.getLineItem();
for (Iterator iterator = lineItemSet.iterator(); iterator.hasNext();) {
LineItem lineItem = (LineItem) iterator.next();
System.out.println("lineItem.getQuantity() = " + lineItem.getQuantity());
}
}
As I can see from the following logs(first query), it joins three tables as expected. However, while accessing LineItem from Order, it is again firing the query.
Please note that it works fine with 2 tables.(Order & LineItem and LineItem & Item). The problem comes with 3 tables. Any help on this would be highly appreciated.
The relevant logs:
Code:
Hibernate: select this.ORDERID as ORDERID2_, this.EMPLOYEENAME as EMPLOYEE2_2_, this.PHONENUM as PHONENUM2_, this.ORDERDATE as ORDERDATE2_, x0_.LINEITEMID as LINEITEMID0_, x0_.ITEMID as ITEMID0_, x0_.ORDERID as ORDERID0_, x0_.QUANTITY as QUANTITY0_, item2_.ID as ID1_, item2_.NAME as NAME1_, item2_.DESCRIPTION as DESCRIPT3_1_, item2_.COST as COST1_ from SNACKORDER this, SNACKORDERITEM x0_, SNACKITEM item2_ where 1=1 and this.ORDERID=x0_.ORDERID and x0_.ITEMID=item2_.ID(+)
order.getEmployeeName() = Pradeep J
Hibernate: select lineitem0_.LINEITEMID as LINEITEMID__, lineitem0_.ORDERID as ORDERID__, lineitem0_.LINEITEMID as LINEITEMID0_, lineitem0_.ITEMID as ITEMID0_, lineitem0_.ORDERID as ORDERID0_, lineitem0_.QUANTITY as QUANTITY0_ from SNACKORDERITEM lineitem0_ where lineitem0_.ORDERID=?
lineItem.getQuantity() = 5
lineItem.getQuantity() = 2
order.getEmployeeName() = Pradeep J
Hibernate: select lineitem0_.LINEITEMID as LINEITEMID__, lineitem0_.ORDERID as ORDERID__, lineitem0_.LINEITEMID as LINEITEMID0_, lineitem0_.ITEMID as ITEMID0_, lineitem0_.ORDERID as ORDERID0_, lineitem0_.QUANTITY as QUANTITY0_ from SNACKORDERITEM lineitem0_ where lineitem0_.ORDERID=?
lineItem.getQuantity() = 1
The mappings:
Order :
Code:
<hibernate-mapping>
<class name="snacks.Order" table="SNACKORDER" proxy="snacks.Order">
<id name="orderID" column="ORDERID" type="int">
<generator class="sequence">
<param name="sequence">SNACKORDER_SEQ</param>
</generator>
</id>
<set name="lineItem" lazy="true">
<key column="ORDERID"/>
<one-to-many class="snacks.LineItem"/>
</set>
<property name="employeeName" column="EMPLOYEENAME" type="string"/>
<property name="phoneNumber" column="PHONENUM" type="int"/>
<property name="orderDate" column="ORDERDATE" type="date"/>
</class>
</hibernate-mapping>
LineItem:
Code:
<hibernate-mapping package="snacks">
<class name="LineItem" table="SNACKORDERITEM" proxy="LineItem">
<id name="lineItemID" column="LINEITEMID" type="string">
<generator class="assigned"/>
</id>
<many-to-one name="item" column="ITEMID" outer-join="false"/>
<many-to-one name="order" column="ORDERID" not-null="true"/>
<property name="quantity" column="QUANTITY" type="int"/>
</class>
</hibernate-mapping>
Item:
Code:
<class name="Item" table="SNACKITEM" proxy="Item">
<id name="itemID" column="ID" type="int">
<generator class="sequence">
<param name="sequence">SNACKITEM_SEQ</param>
</generator>
</id>
<property name="itemName" column="NAME" type="string"/>
<property name="itemDesc" column="DESCRIPTION" type="string"/>
<property name="itemCost" column="COST" type="double"/>
</class>