• 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

Please help: What is the best strategy to update the DB using a detached object?

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have read TFM and searched many times, so if I miss something, please kindly point me out, I'll appreciate it. You can be rude if you want, just point me to somewhere I can start--if nobody replies I don't even know it's because I miss a TFM, or my question is too banal that nobody wants to deign to answer it, or what. Thank you.

My question is this:

On the DB I have a Parent object, which has many children. In Java it's modeled as a Set, with cascade set to all-delete-orphan, and lazy=false:


So I get this from DB, I populate Struts ActionForm with this, and let the user edit Parent and remove/add/edit children as well. When I want to persist it back to the DB is where I get the problem. I have several alternatives:

1. Populate a new empty Parent instance using the ActionForm's values, then call saveOrUpdate on the populated Parent. It works great for editing, also there's only one Hibernate DB call. However, this doesn't work when the user _deleted_ a child in the UI layer. Hibernate will update children that are updated, but will ignore the fact that some children have been deleted.

2. Load a Parent instance first using the id from the ActionForm. Then populate this Parent instance using the values from the ActionForm. Then call saveOrUpdate(). This one is problematic because I cannot say "setChildren()".

I have to say,



But with this approach I need to load an instance first just to edit it. But deletion of children is successfully committed to the DB.

3. Just like #1, instead of using saveOrUpdate, use merge(). HOWEVER... due to the fact that the setChildren() looks like this:



instead of this:



I get this:



So there is conflicting requirement here! First I need to set the Parent of Child while adding, but second I need to leave the set method very simple.

So that makes me wonder. Surely people have been doing this for years. What is the typical practice of doing this?

Thank you!
Ray
 
Ranch Hand
Posts: 662
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you might want to take a look at object state (10.6) and transitive persistence (10.11) in Hibernate.
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is the exact problem I met, if you have it solved,
it's appreciated to keep me updated.

BTW, I read the manual, and add "all,delete-orphan" on the
parent's hbm.xml,but it doesn't work
 
Ray Xie
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Patrick Tsang:
This is the exact problem I met, if you have it solved,
it's appreciated to keep me updated.

BTW, I read the manual, and add "all,delete-orphan" on the
parent's hbm.xml,but it doesn't work



Hey Patrick, yeah... it's a wonder isn't it? I mean this is supposed to be a common scenario--you get object from DB, you transfer to UI, you update, then you bring back to put into DB.

However when you ask the question about this nobody seems to know the answer or want to answer. Did I miss anything? Apparently not, since I had the chance to talk to somebody experienced in Hibernate face-to-face and he confirmed that that was a problem.

In the end I use the approach where I've gotta load an instance from the DB, modify that attached instance, and then save it back. Merge() is supposed to do this cleanly, which is why I wonder again: what is the use of merge() then?

I asked this question also in the Hibernate Forum and didn't get answer--which was real frustrating because obviously the Hibernate people know the answer, so is it because it is too obvious for them to answer? Or what? What I know is that I have RTFM-ed and searched. Sigh.
 
Patrick Tsang
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, I asked the same question in Hibernate forum, but
it looks there is nobody gave a solution. Here is the link

http://forum.hibernate.org/viewtopic.php?t=962069

My current workd-around is to delete the existing data manually,
it'd kind of dumb, but at least it works.

//NOTE: Delete the exisiting students
Query q = session.createQuery("DELETE FROM Student
WHERE teacherid = :teacherId");
q.setInteger("teacherId",teacherId);
q.executeUpdate();
 
reply
    Bookmark Topic Watch Topic
  • New Topic