-->
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: How to update parent without deleting child collection?
PostPosted: Thu Feb 14, 2008 12:20 pm 
Newbie

Joined: Sat Dec 15, 2007 4:11 pm
Posts: 1
Hi all!

I'm writing web application using Hibernate 3.2 with Spring 2.5.

My problem is that when I update parent object the child collection is deleted, because when I get new parameters for the parent from html form I create new instance of a parent which has empty collection. I use merge to update the object.

I have class Book which has a set of Tag elements. The relationship is many-to-many, and I'm using a join table Tagged_books.

The mapping is the following
Book.hbm.xml
Code:
<class name="com.mylibrary.model.Book" table="Books">
...
<set
             name="tags"
             table="TAGGED_BOOKS"
             cascade="all-delete-orphan">

           <key column="BOOK_ID" />
           <many-to-many
                column="TAG_ID"
                class="Tag" />
</set>   

Tag.hbm.xml
Code:
<class name="com.mylibrary.model.Tag" table="Tags">
...
<set name="books"
             inverse="true"
             table="TAGGED_BOOKS" >
           <key column="TAG_ID" />
           <many-to-many
                 column="BOOK_ID"
                 class="Book" />
        </set>


The code in service method:

Code:
public void updateBook(Book book) {
      HibernateTemplate hibernateTemplate = getHibernateTemplate();
       hibernateTemplate.setFlushMode(HibernateAccessor.FLUSH_EAGER);
       hibernateTemplate.merge(book);
   }


I use OpenSessionInViewFilter to bound session to current thread.
The database is HSQLDB
Is it possible to update a parent only without changes to it's collections?

Thank you very much for help.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 14, 2008 3:38 pm 
Senior
Senior

Joined: Fri Jun 01, 2007 12:41 pm
Posts: 121
You can not update only Parent object excluding child from Hibernate. The only solution to your problem is:

Read the Book class from with lazy loading for Tags. Then set the properties in Book with you html form properties and send it to Hibernate.

This way only Book object is updated.


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.