-->
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.  [ 1 post ] 
Author Message
 Post subject: Session.merge() and Session.update() behave differently
PostPosted: Mon Aug 11, 2014 3:45 pm 
Newbie

Joined: Mon Aug 11, 2014 3:38 pm
Posts: 1
Session.merge() and Session.update() behave differently for cascade="all-delete-orphan" relationships

I have the following entity relationship setup.
Code:
class Parent
{
  //inverse="true" cascade="all-delete-orphan" (hbm)
  Set<Child> children;
}


Code:
class Child {
  //many-to-one (hbm)
  Parent parent;

  //inverse="true" cascade="all-delete-orphan" (hbm)
  Set<GrandChild> grandChildren;
}


Code:
class GrandChild {
  //many-to-one
  Child child;
  String name;
}


My application load's the parent class instance and sends it to the UI in one transaction and does one of the following below before calling saveOrUpdate() in a different transaction.
>When updating a grand child's name and calling Session.saveOrUpdate(Parent) the child's name is updated due to cascade
>When adding a new grand child in the Child's set and calling
Session.saveOrUpdate(Patent) the new child is added due to cascade
>But When removing grand child element from the child and then doing a
Session.saveOrUpdate(Parent) doesn't remove the child in fact the version column of the that grandchild record is untouched,
Session.update(Parent) also doesn't remove the grand child.
but doing Session.merge(Parent) does remove the grand child
Is this correct behavior? I thought if "all-delete-orphan" option is given then it should cascade regardless of it being a merge() or update().
I am using Spring Framework's HibernateTemplate.merge(), HibernateTemplate.update(), HibernateTemplate.saveOrUpdate() functions.


Update: It looks like the issue is with the way i was updating the Set collections.
for session.saveOrUpdate() or session.update() to cascade properly you need to either use collection.remove() or collection.clear() followed by collection.set();

If you do just collection.set() it does not remove items from the collection.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.