-->
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.  [ 2 posts ] 
Author Message
 Post subject: One-to-many bi-directional mapping problem
PostPosted: Tue Jul 12, 2005 10:47 pm 
Newbie

Joined: Mon Mar 21, 2005 11:54 pm
Posts: 3
Location: Salt Lake City, UT
Hibernate version: 2.18

Mapping documents:

CategoryImpl.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping package="com.wasatchcottage.domain">

<class name="CategoryImpl" table="category" proxy="com.wasatchcottage.domain.Category" dynamic-update="true" dynamic-insert="false" lazy="false">
<id name="id" type="int" column="catg_sysid" unsaved-value="-1">
<generator class="identity">
<param name="sequence">catg_sysid_sequence</param>
<param name="table">HIVAL</param>
<param name="column">nextCategoryId</param>
</generator>
</id>

<property name="name" column="catg_name" type="string" length="64" not-null="true"/>
<property name="description" column="catg_desc" type="text"/>
<property name="image" column="catg_image" type="string" length="128"/>
<many-to-one name="parent" column="catg_parent_id" class="com.wasatchcottage.domain.CategoryImpl"/>

<set name="parents" cascade="all" inverse="true" lazy="true">
<key column="catg_sysid"/>
<one-to-many class="com.wasatchcottage.domain.CategoryImpl"/>
</set>

<property name="sortOrder" column="catg_sort_order" type="int"/>
<property name="modifyDate" column="catg_mod_date" type="timestamp" not-null="true"/>
<property name="createDate" column="catg_create_date" type="timestamp" not-null="true"/>
<property name="status" column="catg_status" type="boolean" not-null="true"/>

<set name="products" cascade="save-update" inverse="true" lazy="true" outer-join="false">
<key column="prod_sysid"/>
<one-to-many class="com.wasatchcottage.domain.ProductImpl"/>
</set>

</class>

<query name="findCategoryByParentNameOrderByName"><![CDATA[
from com.wasatchcottage.domain.Category as category where category.parent.name = ? order by category.name
]]>
</query>

</hibernate-mapping>

ProductImpl.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping package="com.wasatchcottage.domain">

<class name="ProductImpl" table="product" proxy="com.wasatchcottage.domain.Product" dynamic-update="true" dynamic-insert="false" lazy="false">
<id name="id" type="int" column="prod_sysid" unsaved-value="-1">
<generator class="identity">
<param name="sequence">prod_sysid_sequence</param>
<param name="table">HIVAL</param>
<param name="column">nextProductId</param>
</generator>
</id>

<property name="productId" column="prod_id" type="string" length="20" not-null="true" unique="true"/>
<property name="name" column="prod_name" type="string" length="64" not-null="true"/>
<property name="description" column="prod_desc" type="text"/>
<property name="qtyOnHand" column="prod_qty_on_hand" type="int" length="5"/>
<property name="image" column="prod_image" type="string" length="128"/>
<property name="price" column="prod_price" type="java.math.BigDecimal" length="2"/>
<property name="weight" column="prod_weight" type="java.math.BigDecimal" length="2"/>
<many-to-one name="taxClass" class="com.wasatchcottage.domain.TaxClassImpl" column="prod_txcl_sysid" not-null="true"/>
<property name="modifyDate" column="prod_modify_date" type="timestamp" not-null="true"/>
<property name="createDate" column="prod_create_date" type="timestamp" not-null="true"/>
<property name="status" column="prod_status" type="boolean" not-null="true"/>

<many-to-one name="category" class="com.wasatchcottage.domain.CategoryImpl" column="prod_catg_sysid"
outer-join="auto"
update="false"
insert="false"
cascade="save-update"
unique="false"
/>

</class>

</hibernate-mapping>


Name and version of the database you are using: MySQL

The generated SQL (show_sql=true):

Hibernate: select products0_.prod_sysid as prod_sysid__, products0_.prod_sysid as prod_sysid3_, products0_.prod_id as pr
od_id3_, products0_.prod_name as prod_name3_, products0_.prod_desc as prod_desc3_, products0_.prod_qty_on_hand as prod_q
ty5_3_, products0_.prod_image as prod_image3_, products0_.prod_price as prod_price3_, products0_.prod_weight as prod_wei
8_3_, products0_.prod_txcl_sysid as prod_txc9_3_, products0_.prod_modify_date as prod_mo10_3_, products0_.prod_create_da
te as prod_cr11_3_, products0_.prod_status as prod_st12_3_, products0_.prod_catg_sysid as prod_ca13_3_, taxclassim1_.txc
l_sysid as txcl_sysid0_, taxclassim1_.txcl_title as txcl_title0_, taxclassim1_.txcl_description as txcl_des3_0_, taxclas
sim1_.txcl_mod_date as txcl_mod4_0_, taxclassim1_.txcl_create_date as txcl_cre5_0_, taxclassim1_.txcl_status as txcl_sta
6_0_, categoryim2_.catg_sysid as catg_sysid1_, categoryim2_.catg_name as catg_name1_, categoryim2_.catg_desc as catg_des
c1_, categoryim2_.catg_image as catg_image1_, categoryim2_.catg_parent_id as catg_par5_1_, categoryim2_.catg_sort_order
as catg_sor6_1_, categoryim2_.catg_mod_date as catg_mod7_1_, categoryim2_.catg_create_date as catg_cre8_1_, categoryim2_
.catg_status as catg_sta9_1_, categoryim3_.catg_sysid as catg_sysid2_, categoryim3_.catg_name as catg_name2_, categoryim
3_.catg_desc as catg_desc2_, categoryim3_.catg_image as catg_image2_, categoryim3_.catg_parent_id as catg_par5_2_, categ
oryim3_.catg_sort_order as catg_sor6_2_, categoryim3_.catg_mod_date as catg_mod7_2_, categoryim3_.catg_create_date as ca
tg_cre8_2_, categoryim3_.catg_status as catg_sta9_2_ from product products0_ left outer join tax_class taxclassim1_ on p
roducts0_.prod_txcl_sysid=taxclassim1_.txcl_sysid left outer join category categoryim2_ on products0_.prod_catg_sysid=ca
tegoryim2_.catg_sysid left outer join category categoryim3_ on categoryim2_.catg_parent_id=categoryim3_.catg_sysid where
products0_.prod_sysid=?

Problem:

As you can see from the above, there is a one-to-many bi-directional relationship between the CategoryImpl and the ProductImpl. Everything in the configuration seems to look and work fine. However, the following code excerpt does not return the correct dataset.

Set products = category.getProducts();

There should be two products associated with the category in question, but only one is returned. Furthermore, if I manually change the database to exclude the one product that is getting returned, it is still returned.

I am pretty new to Hibernate, so any information regarding my configuration (or problems with it), please let me know.

Lastly, I am using Hibernate in a Spring framework environment, so I have included my Hibernate Spring configuration.

dataAccessContext.xml

<bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
<property name="dataSource"><ref bean="dataSource"/></property>
<property name="mappingResources">
<list>
<value>CategoryImpl.hbm.xml</value>
<value>ProductImpl.hbm.xml</value>
.
.
.
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">net.sf.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.query.substitutions">true=1 false=0</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 12, 2005 11:48 pm 
Newbie

Joined: Mon Mar 21, 2005 11:54 pm
Posts: 3
Location: Salt Lake City, UT
I resolved the issue. It was a stupid mistake.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.