2

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);
            }
            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?

1 Answer 1

3

Don't fetch them, just use some criteria for deletion. Use HQL for such cases

session.createQuery("delete from MyClass where ...").setXXX(...).executeUpdate();
Sign up to request clarification or add additional context in comments.

4 Comments

So if the HQL query return 2000 objects, it will not have hibernate or memory issue?
When you delete it using HQL they are not loaded they just got deleted.
And if my HQL delete must delete 276 000 objects, it will works? :D
It will work with any amount of objects. Hibernate will simply convert the command to SQL and pass it to the database, no objects will be loaded.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.