• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

One-to-many relationship is updating entire child table with new parent id

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 53
MyEclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
change the query or change bidirectional mapping
 
deepa karkala
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you be please more specific. I need to call "save" method to insert the rows right ? I can only call save. Internally in the log I can see hibernate calls save or update.

Change the bi-directional mapping means ? I was having uni directional mapping and as I mentioned it was updating all old records in the table with new parent id. That's why I changed it to bi-directional.

Appreciate if you provide me more information.

thanks.
reply
    Bookmark Topic Watch Topic
  • New Topic