-->
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.  [ 6 posts ] 
Author Message
 Post subject: Bulk delete with native SQL problem
PostPosted: Wed Jun 23, 2004 10:44 am 
Beginner
Beginner

Joined: Wed Jan 21, 2004 10:15 am
Posts: 24
Location: Munich, Germany
Hello!

I have a problem with a native SQL "Query" which is actually no query (i.e. a SELECT statement), but rather a DELETE statement.

I have to delete a lot of entries of a database. My first try was something like
Code:
Query q = session.createQuery("from Cat cat where cat.id<1000");
for (Iterator i = q.iterate(); i.hasNext(); ) {
  session.delete(i.next());
}


With this approach, all the cat objects that are to be deleted will be loaded first, which results in bad performance (there seem to be different opinions whether this is good or not). So I tried to use another approach with native SQL queries:

Code:
Query q = session.createQuery("from Cat cat where cat.id<1000");
for (Iterator i = q.iterate(); i.hasNext(); ) {
  Query delQuery = session.createSQLQuery("delete from cat {cat} where cat.id=:id", "cat", Cat.class);
  delQuery.setLong("id", ((Cat)i.next()).getId());
  delQuery.list(); // Ooops!
}


Now, this code crashes, because finally I end up with a JDBC Statement.executeQuery("delete ..."), which is of course wrong: It should be Statement.execute() instead.

Is there another possibility except Session.list() and Session.iterate() to execute a query, which results in a Statement.execute() instead of a Statement.executeQuery()?

Or can this be done in another, efficient way?

Thanks!
Christian


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 23, 2004 11:26 am 
Senior
Senior

Joined: Sun Jan 04, 2004 2:46 pm
Posts: 147
Use session.connection() and execute via JDBC. There is no way to do bulk insert/update/delete though the hibernate framework.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 24, 2004 4:46 am 
Beginner
Beginner

Joined: Wed Jan 21, 2004 10:15 am
Posts: 24
Location: Munich, Germany
Okay.

One more question: Is there any way to get the name of the mapped table when I have a Session object and the Class object of the mapped class? I could get a ClassMetadata object, but that does not seem not to contain the desired information. I think I have to get a PersistentClass object. Does anybody know how to get that?

Thanks,
Christian


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 24, 2004 4:50 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Beware, bad hack: http://www.gloegl.de/26.html


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 24, 2004 5:25 am 
Beginner
Beginner

Joined: Wed Jan 21, 2004 10:15 am
Posts: 24
Location: Munich, Germany
I have now decided to hard code the table name... Bad, but that seems to be the only option (besides a bad hack ;-)

One more question: When I get a direct JDBC Connection via session.connection(), do I have to bear something in mind? Like closing, not closing, returning it to the Session, etc?

Christian


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 24, 2004 5:53 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Don't do anyhing withit, just close the session at the end.


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