Sunday, October 6, 2013

Insert large data sets with Hibernate

Hibernate is a generic ORM framework and when it comes to very specific scenarios you may face difficulties. Specially when try to insert/write large data set(more than 1 million records) to the database at once.

But there are ways to get away from that

Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();

int i = 0;
for (Iterator<T> iterator = persistenceObject.iterator(); iterator.hasNext();) {
  i++;
  session.saveOrUpdate(iterator.next());
  if (i % 50 == 0) {
session.flush();
session.clear();
  }
}
tx.commit();

session.close();


Above example write 50 records at once not 1 million at once. If you have more efficient ways please comment on this