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>
|