-->
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.  [ 3 posts ] 
Author Message
 Post subject: Batch delete
PostPosted: Fri Jan 16, 2009 2:45 am 
Newbie

Joined: Thu Oct 25, 2007 5:24 pm
Posts: 12
Hi,
What is the best approach to deleting a bunch of rows from a table by primary key?

currently i have this going on, but is there some shortcuts?
Code:
for (NodeElement node : deleteCandidates){
   Query query = this.entityMgr.createNativeQuery("delete from node where id = :id");
   query.setParameter("id", node.getId());
   query.executeUpdate();
   if (++i % FLUSH_SIZE == 0){
      entityMgr.flush();
      entityMgr.clear();
   }      
}   


is building a giant "WHERE id IN ( " + node.getId() ", )" ... any better? I may need to delete hundreds or thousands or rows, but not ready for a stored procedure.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 16, 2009 3:56 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
I think you should at least move the call to createNativeQuery() outside the loop. I don't think there is any reason to flush() or clear(). The queries are only affecting the database not the in-memory state of objects in the session/entity manager.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 16, 2009 6:07 am 
Newbie

Joined: Thu Jun 26, 2008 8:59 am
Posts: 9
Location: Dublin, Ireland
Maybe this chapter could help you http://www.hibernate.org/hib_docs/v3/re ... batch.html. It helped me when doing something similar.


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