-->
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.  [ 5 posts ] 
Author Message
 Post subject: One-to-Many association. Issue with fetch left join
PostPosted: Mon Jun 21, 2010 2:24 pm 
Newbie

Joined: Mon Jun 21, 2010 1:36 pm
Posts: 14
Hi All,

I am coming across an issue in my sample application. Please help.

I have 4 objects in my sample application: Customer, Order, OrderItem, Product and their associations are as follows:
Customer --> Order (One-to-Many Bi-directional)
Order --> OrderItem (One-to-Many Uni-directional)
OrderItem --> Product (Many-to-One Uni-directional)

And the 4 tables are CUSTOMER, C_ORDER, ORDER_ITEM, PRODUCT.

Through my application I created an order with 2 order items. This created 1 record in C_ORDER table and 2 records in ORDER_ITEM table. Please note that this is the only Order record in the DB. Then I tried to fetch details of all orders in my application along with its order items with eager loading using the following statement:

List<Order> orderList = session.createQuery(“from Order o left join fetch o.orderItems”).list();

The above statement has returned 2 Order records in the list. Both these records contain the same data. And there is only one Order in the DB. I captured the SQL generated by Hibernate and it is as below:
select order0_.ORDER_ID as ORDER1_1_0_, orderitems1_.ORDER_ITEM_ID as ORDER1_2_1_, order0_.ORDER_TOTAL as ORDER2_1_0_, order0_.CUSTOMER_ID as CUSTOMER3_1_0_, orderitems1_.ORDER_ITEM_QUANTITY as ORDER2_2_1_, orderitems1_.PRODUCT_ID as PRODUCT3_2_1_, orderitems1_.ORDER_ID as ORDER4_0__, orderitems1_.ORDER_ITEM_ID as ORDER1_0__ from C_ORDER order0_ left outer join ORDER_ITEM orderitems1_ on order0_.ORDER_ID=orderitems1_.ORDER_ID

When I ran the above query in the HSQL DB manager, it returned two records as below:
ORDER1_1_0_ ORDER1_2_1_ ORDER2_1_0_ CUSTOMER3_1_0_ ORDER2_2_1_ PRODUCT3_2_1_ ORDER4_0__ ORDER1_0__
----------- ----------- ----------- -------------- ----------- ------------- ---------- ----------
1 1 0.0 1 2 2 1 1
1 2 0.0 1 1 1 1 2

2 row(s) in 1 ms
which seemed correct. I expected that the above 2 records should have been mapped by Hibernate into one Order object containing two OrderItem objects. But surprisingly two Order objects were returned by my HQL query. I wonder why this is happening.

I am attaching the hbm.xml files below for reference:

Customer.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<class name="com.bpp.cm.Customer" table="CUSTOMER">
<id name="id" type="long" column="CUSTOMER_ID">
<generator class="native"/>
</id>
<property name="name" type="string" column="CUSTOMER_NAME"/>
<property name="phone" type="string" column="CUSTOMER_PHONE"/>

<set name="orders" inverse="true">
<key column="CUSTOMER_ID"/>
<one-to-many class="com.bpp.cm.Order"/>
</set>
</class>
</hibernate-mapping>


Order.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<class name="com.bpp.cm.Order" table="C_ORDER">
<id name="id" type="long" column="ORDER_ID">
<generator class="native"/>
</id>
<property name="total" type="float" column="ORDER_TOTAL"/>
<many-to-one name="customer" column="CUSTOMER_ID" class="com.bpp.cm.Customer" not-null="true"/>
<set name="orderItems" cascade="all">
<key column="ORDER_ID"/>
<one-to-many class="com.bpp.cm.OrderItem"/>
</set>
</class>
</hibernate-mapping>


OrderItem.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<class name="com.bpp.cm.OrderItem" table="ORDER_ITEM">
<id name="id" type="long" column="ORDER_ITEM_ID">
<generator class="native"/>
</id>
<property name="quantity" type="integer" column="ORDER_ITEM_QUANTITY"/>
<many-to-one name="product" column="PRODUCT_ID" class="com.bpp.cm.Product" lazy="false"/>
</class>
</hibernate-mapping>


Product.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<class name="com.bpp.cm.Product" table="PRODUCT">
<id name="id" type="long" column="PRODUCT_ID">
<generator class="native"/>
</id>
<property name="name" type="string" column="PRODUCT_NAME"/>
<property name="price" type="float" column="PRODUCT_PRICE"/>
</class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject: Re: One-to-Many association. Issue with fetch left join
PostPosted: Tue Jun 22, 2010 8:18 am 
Newbie

Joined: Fri May 21, 2010 8:37 am
Posts: 14
I guess you are using eager fetchtype ?

Then you'll resolve it by either removing eager and get the set when you need it, or use the "DistinctResultTransformer.INSTANCE.transformList"

EDIT:

OK - note to self, learn to read the complete post prior to posting. You are not using eager, so forget what I wrote above


Top
 Profile  
 
 Post subject: Re: One-to-Many association. Issue with fetch left join
PostPosted: Tue Jun 22, 2010 9:59 am 
Newbie

Joined: Mon Jun 21, 2010 1:36 pm
Posts: 14
Perlovmo,

Thanks for your response. My scenario is that in general I want to lazy load OrderItems in an Order. But in a particular function I would like to eagerly load the OrderItems. So, I used join as part of my query in this particular function. This is baed on example given on page 587 in JAVA PERSISTENCE WITH HIBERNATE book.

I am thinking I will try out the same thing on a simpler example with just 2 entities and see. Meanwhile, if you find something please let me know.

Thanks.


Top
 Profile  
 
 Post subject: Re: One-to-Many association. Issue with fetch left join
PostPosted: Tue Jun 22, 2010 11:24 am 
Regular
Regular

Joined: Thu Oct 19, 2006 12:07 pm
Posts: 75
bjagan this is a FAQ. See here:
http://web.archive.org/web/200702021455 ... 7.html#A12

To the webmaster: why does the current site not have a FAQ page???


Top
 Profile  
 
 Post subject: Re: One-to-Many association. Issue with fetch left join
PostPosted: Wed Jun 23, 2010 1:30 am 
Newbie

Joined: Mon Jun 21, 2010 1:36 pm
Posts: 14
xerces,

Thanks a lot! This FAQ exactly answers my question. But I somehow feel this behavior from Hibernate, for that matter from any ORM is not correct. When mapping/converting the data from SQL Resultset into objects, I guess this could have been taken care.

Thanks again!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.