posted 13 years ago
Hi, I am using spring LocalSessionFactoryBean and HibernateTransactionManager to carry out one-to-many operation in Oracle XE 10g database.
What is happening is, each time when I add a parent record with bunch of child records, getHibernateTemplate().save(parentRec) updating the current parend id for all other old records
in the child table.
This forced me to define many-to-one relationship in the child class config file which forces me to define a property for parent class in Child class which I do not want to do.
I did not want to define many-to-one relationship in the child class config file.
Parent record hbm.xml :
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.rss.beans">
<class name="Application" table="application">
<meta attribute="class-description">This class contains application details.</meta>
<id name="appid" type="long" column="appid" unsaved-value="0">
<generator class="sequence">
<param name="sequence">App_Seq</param>
</generator>
</id>
<property name="appName" type="string" not-null="true" length="100" column="appname" />
<set name="components" cascade="all" lazy="false" inverse="true" >
<key column="appid" not-null="true" />
<one-to-many class="Component"/>
</set>
</class>
</hibernate-mapping>
Child record hbm.xml : If I remove <many-to-one> in the below xml, the entire child table will be updated with current parent id. Very annoying.
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.rss.beans">
<class name="Component" table="component">
<id name="compid" column="compid" type="long" unsaved-value="0">
<generator class="sequence">
<param name="sequence">Comp_Seq</param>
</generator>
</id>
<property name="compName" type="string" length="20"/>
<many-to-one name="app" class="Application" >
<column name="appid" not-null="true" />
</many-to-one>
</class>
</hibernate-mapping>
Did anyone face this issue earlier ? Does it mean that in hibernte we can only define bi-directional One-to-Many relatinship ? I refered so many posts but no where mentioned about this weird issue.
Any suggestion is helpful.
thanks