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.