开发者

Hibernate optimization for delete severals objects

开发者 https://www.devze.com 2023-04-10 18:22 出处:网络
In a Java EE application with hibernate, in a big table, I want to purge some millions of objects. For example

In a Java EE application with hibernate, in a big table, I want to purge some millions of objects.

For example

  1. I get some objects that i want to purge: 2000 objects returned over 5000 objets in the tab
  2. 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.

  1. What is the best solution in order to remove 2000 objects for example and not have memory or hibernate exceptions?

  2. 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();
0

精彩评论

暂无评论...
验证码 换一张
取 消