Curso de Hibernate | Tratamientos batch 1

Curso de Hibernate
Tratamientos batch

Curso de Hibernate | Tratamientos batch 2

No podemos tener en la sesión de hibernate demasiados objetos porque nos quedaríamos sin memoria. en su lugar haremos un procesamiento por lotes, procesando los registros de 20 en 20 y liberando la sesión

hibernate.cfg.xml

<property name="hibernate.jdbc.batch_size">20</property>
<property name="hibernate.cache.use_second_level_cache">false</property>

Main.java

public class Main {
 public static void main(String[] args) {

  Session session = HibernateUtil.getSessionFactory().openSession();
  Transaction tx = session.beginTransaction();

  for (int i = 0; i < 100000; i++) {
   Libro l = new Libro();
   l.setTitulo("El Quijote " + i);

   session.save(l);
   if (i % 20 == 0) { // 20, same as the JDBC batch size
    // flush a batch of inserts and release memory:
    session.flush();
    session.clear();
   }
  }

  tx.commit();
  session.close();

 }
}

Documentación oficial:

https://docs.jboss.org/hibernate/orm/5.2/userguide/html_single/chapters/batch/Batching.html