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();) {
  if (i % 50 == 0) {


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

No comments:

Post a Comment