File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Maintaining create/delete/update actions on child records in Hibernate across different sessions

 
Yn Rao
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Hibernate Gurus,

I have a parent/child relationship mapped in hibernate just like it is explained in hibernate reference chapter 24

http://docs.jboss.org/hibernate/stable/core/reference/en/html/example-weblog.html

My problem is I have a form where I display a blog with all of its items. The user can update/delete and create new items in the form all in one action (HTTP POST)

So for example, lets say Blog with name="Hibernate" has 3 blog items, item1, item2 and item3

The database is queried to fetch the blog and its items initially and the blog is evicted from the session so that a new blog object with same id can be attached later.

That information is shown in a HTML form to the user, and the user takes the following actions in the form.

1. deletes item1 (HTML elements corresponding to item1 are removed using javascript)
2. updates item2 (changes the text for example)
3. And created a new item4. (using a dynamically generated required form input elements) and submits the form.

When the form is submitted, I am able to save the updated item2 and the new item4 using saveOrUpdate() on the new Blog object constructed from the browser fields.

However I am not able to have the item1 deleted automatically.

One way (expensive way) to accomplish the deletion of item1 is to read the Blog from db and check for any missing items in the item array that came from the browser.

Is that the only way to achieve this? Does hibernate provide any magic to solve this elegantly?

Thanks
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and the blog is evicted from the session so that a new blog object with same id can be attached later


Wouldn't a blog object with the same id be the same blog?
 
Yn Rao
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually I had to call evict(), with out that call I was getting the following exception.

org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session:

Good that you asked, This is another problem I wanted to check. It does not really make sense to clear the cache to store a new modified Blog object.

Many online hibernate tutorials have documented this kind of saving detached objects with out calling evict(). Am I missing something?

 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic