Hibernate optimization for delete severals objects
In a Java EE application with hibernate, in a big table, I want to purge some millions of objects.
For example
- I get some objects that i want to purge: 2000 objects returned over 5000 objets in the tab
- and then I want to remove them
Actually while object exists i do:
List<Object> objectList = this.getObjectManager()
.getObjectsByCriteria(clientName, objectType, MAX_OBJECTS);
for (final Object object : objectList) {
if (logger.isDebugEnabled()) {
logger.debug("will delete " + object);
开发者_如何学Go }
this.getMManager().removeEntry(object);
counter++;
}
public void removeObject(final Object object) {
final Session session = this.getHibernateUtil().getSession();
session.delete(entry);
session.flush();
}
I guess that hibernate removes all objects at the commit of the transaction, and not one by one.
What is the best solution in order to remove 2000 objects for example and not have memory or hibernate exceptions?
How to remove really one by one with hibernate?
Don't fetch them, just use some criteria for deletion. Use HQL for such cases
session.createQuery("delete from MyClass where ...").setXXX(...).executeUpdate();
精彩评论