-->
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.  [ 9 posts ] 
Author Message
 Post subject: Deleting a sub-collection strategy
PostPosted: Mon Dec 20, 2004 3:33 pm 
Beginner
Beginner

Joined: Tue Oct 19, 2004 11:04 am
Posts: 22
I have two classes (A and B) where A has a List of B.

Code:
class A {

    private java.util.List bList;

    // other members and methods not important
}

class B {
    // members and methods not important
}


The database tables for these two classes would be:

Code:
create table A (
    id VARCHAR(36) NOT NULL,
    // other A columns
)

create table B (
    id VARCHAR(36) NOT NULL,
    a_id VARCHAR(36) NOT NULL,
    // other B columns
)


In the 'B' table, the a_id has a foreign key relationship with the id column of the 'A' table.

My question is this: I have a detached instance of A that is populated with several instances of B in its bList. I want to delete all of the instances of B from the instance of A without deleting A. If I were writing strait JDBC code, I would use the following SQL:

Code:
delete from B where a_id = [the id value of A's id];


What is the best way to do the same with Hibernate? I know of two ways, neither of which is attractive:

Method #1:
Code:
    session.delete("from B as b where b.aId = " + a.id);


The problem with this is that all of the Bs will first be selected from the database before they are deleted. This seems silly because I already have all of the B data in memory within A's bList (remember that I have detached instances). Why would I want to re-select them just to delete them?

Method #2:
Code:
    for (Iterator i = a.getBList(); i.hasNext(); ) {
        B b = (B)i.next();
        session.delete(b);
    }


This would work fine, except that it will issue one SQL delete statement per instance of B. This also seems silly considering that if I were using JDBC, I would do the same with one SQL delete statement as described above.

So, finally, how do I make Hibernate remove all of the elements of a collection, without removing the collection's parent, in one single SQL delete call?

Thanks!

Hibernate version:2.1.7


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 20, 2004 3:35 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Not possible in 2.1, will be possible in next release of 3.0.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 20, 2004 3:39 pm 
Beginner
Beginner

Joined: Tue Oct 19, 2004 11:04 am
Posts: 22
Thanks for your quick response!

I was afraid of that. Just for curiosity's sake, what will be the method for doing this when 3.0 is released?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 20, 2004 4:26 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
ejb3 style delete by query.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 20, 2004 5:41 pm 
Newbie

Joined: Sat Nov 20, 2004 10:16 pm
Posts: 8
Location: Canada
While there is no stable version 3.0, what would be the best way for doing this? I tried method #1 above but it didnt work . I get the following error:

net.sf.hibernate.ObjectDeletedException: deleted object would be re-saved by cascade (remove deleted object from associations): 1363, of class: sonicBiz.DefDNS

if I remove the object from the collection I get:

java.util.ConcurrentModificationException


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 20, 2004 5:58 pm 
Newbie

Joined: Sat Nov 20, 2004 10:16 pm
Posts: 8
Location: Canada
please disregard. Method 1 above did work. I was doing something wrong


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 22, 2004 3:45 am 
Newbie

Joined: Tue Dec 21, 2004 10:53 am
Posts: 8
Location: Stuttgart/Germany
Hi,
can you say me what you did wrong and what is the solution?
I have a problem similar to yours.

Thanks for your answer.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 22, 2004 12:20 pm 
Newbie

Joined: Sat Nov 20, 2004 10:16 pm
Posts: 8
Location: Canada
Well, I was actually trying to update a collection . Class1 contains a collection of Class2. I needed to update that collection and the number of elements of the new collection is not necessarily the same as the first one.

Here is what I did:

Class1 class1 = (Class1)session.get(Class1.class, "somekey");

Iterator it = class1.getListOfClass2().iterator();

while (it.hasNext()){
Class2 class2 = (Class2)it.next();
session.delete(class2);

}

Set set = new HashSet();
set.add(new Class2(parameter1, parameter2,...));
set.add(new Class2(...));
set.add(new Class2(...));
set.add(new Class2(...));
class1.setListOfClass2(set);

session.update(class1);

//just deleted the elements and then assigned a new collection to Class1

I hope it helps


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 29, 2004 11:07 am 
Beginner
Beginner

Joined: Tue Oct 19, 2004 11:04 am
Posts: 22
Quote:

ejb3 style delete by query.



Gavin,

I've been trying to find the semantics on how to do this using Hibernate3.0beta1. Could you briefly explain how to do this...or, better yet, provide link to some docs that explain it?

Thanks!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 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.