This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Object Relational Mapping and the fly likes Maintaining create/delete/update actions on child records in Hibernate across different sessions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Maintaining create/delete/update actions on child records in Hibernate across different sessions" Watch "Maintaining create/delete/update actions on child records in Hibernate across different sessions" New topic
Author

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

Yn Rao
Greenhorn

Joined: Jan 15, 2010
Posts: 2
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

Joined: Aug 26, 2006
Posts: 4968
    
    1

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

Joined: Jan 15, 2010
Posts: 2
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?

 
GeeCON Prague 2014
 
subject: Maintaining create/delete/update actions on child records in Hibernate across different sessions