-->
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.  [ 5 posts ] 
Author Message
 Post subject: delete on collection element performs update on parent
PostPosted: Tue Mar 09, 2004 12:22 am 
Newbie

Joined: Tue Mar 09, 2004 12:01 am
Posts: 9
I have a parent-child relationship mapped using a Set, (one-to-many). When I remove an element from the collection (parent.getChildren().remove(child)) and submit the change to hibernate using session.update(parent) then 2 SQL queries are produced, one updating the parent (every field of it) and one doing the right update on the child (id=null).

I am supposing the update of every field happens because I am updating the parent in another session than the one where I retrieved it (web app).

Is there a way to tell hibernate to commit the collection changes only.



Fabien.

PS: this is with hibernate 2.1 and mysql 3.51 transactional

Sample of code:

Code:
public class Parent  {
// [...]

    /**
     * @hibernate.set cascade="delete"
     *                table="children"
     *                where="type='place'"
     * @hibernate.collection-key column="owner_id"
     * @hibernate.collection-one-to-many 
     *                                   class="ChildImpl"
     */
     Collection getChildren() {
          return children
      }
}




Code:
Session session = sessionFactory.openSession();
Parent parent = (Parent) session.get(Parent.class,"1");
session.close();

// later

parent.getChildren().remove(aChild);
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
session.update(parent);
transaction.commit();
session.close();

// --> 2 SQL statements


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 09, 2004 3:04 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
http://www.hibernate.org/155.html

_________________
Emmanuel


Top
 Profile  
 
 Post subject: no inverse="true" necessary
PostPosted: Tue Mar 09, 2004 3:58 pm 
Newbie

Joined: Tue Mar 09, 2004 12:01 am
Posts: 9
inverse="true" on the set does not solve the problem (although it is problably a good idea in this case).
I just need to do:
Code:
parent.remove(aChild)
aChild.setParentId(parent.getId());
session.update(aChild);


This will not do the best possible update since it will update child name and parentid in the db instead of parentid only.
To better optimize it, it looks like a HQL query would do it.

My problem was not due to inverse="false" but just to the fact that I am doing the update of the parent in another session (it would actually do only one update on the children if asked in 1 session only).

Another way to optimize it would maybe be to use JCS, but I am not sure about it and JCS docs are too short to answer this question.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 09, 2004 4:00 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
For a start, you should not have "setParentId" at all, but "setParent", mapped with a many-to-one relation element.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 09, 2004 4:45 pm 
Newbie

Joined: Tue Mar 09, 2004 12:01 am
Posts: 9
Yes you are probably right. But that does not change things much, I will just have a

Code:
Child aChild = new Child();
parent.add(aChild);
aChild.setParent(parent);
session.save(aChild);

to add a child and

Code:
parent.remove(aChild);
aChild.setParent(null);
session.update(aChild);

to remove a child.

In fact it works the same way with parentId, except that the parent is not accessible through the child, I am even quite sure that cascade delete would work with parentId. One reason why I used parentId was to avoid too many relations, for possible easier serialization, and because I never need to access the parent from the child in my case.

The bad idea was to update the parent when not in the same session (although very practicle when retrieving parent and removing child in the same session).


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

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.