package com.vladmihalcea.book.hpjp.hibernate.flushing;

import com.vladmihalcea.book.hpjp.util.AbstractTest;
import org.junit.Test;

import javax.persistence.*;

/**
 * @author Vlad Mihalcea
 */
public class BatchProcessingTest extends AbstractTest {

    @Override
    protected Class<?>[] entities() {
        return new Class<?>[] {
                Post.class
        };
    }

    @Test
    public void testBatchProcessing() {
        int entityCount = 100;
        int batchSize = 25;

        EntityManager entityManager = null;
        EntityTransaction transaction = null;
        try {
            entityManager = entityManagerFactory().createEntityManager();

            transaction = entityManager.getTransaction();
            transaction.begin();

            for ( int i = 0; i < entityCount; ++i ) {
                Post post = new Post( String.format( "Post %d", i ) );
                entityManager.persist( post );
                if ( i % batchSize == 0 ) {
                    entityManager.flush();
                    entityManager.clear();

                    transaction.commit();
                    transaction.begin();
                }
            }

            transaction.commit();
        } catch (RuntimeException e) {
            if ( transaction != null && transaction.isActive()) {
                transaction.rollback();
            }
            throw e;
        } finally {
            if (entityManager != null) {
                entityManager.close();
            }
        }
        //end::batch-session-batch-insert-example[]
    }

    @Entity(name = "Post")
    @Table(name = "post")
    public static class Post {

        @Id
        @GeneratedValue
        private Long id;

        private String title;


        public Post() {
        }

        public Post(String title) {
            this.title = title;
        }

        public Long getId() {
            return id;
        }

        public void setId(Long id) {
            this.id = id;
        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }
    }
}

@@ -49,16 +49,46 @@
 shing;%0A%0A
+import java.util.Properties;%0A%0A
 import c
@@ -132,16 +132,16 @@
 ctTest;%0A
-
 import o
@@ -400,24 +400,412 @@
   %7D;%0A    %7D%0A%0A
+    @Override%0A    protected Properties properties() %7B%0A        Properties properties = super.properties();%0A        properties.put(%22hibernate.jdbc.batch_size%22, %2225%22);%0A        properties.put(%22hibernate.order_inserts%22, %22true%22);%0A        properties.put(%22hibernate.order_updates%22, %22true%22);%0A        properties.put(%22hibernate.jdbc.batch_versioned_data%22, %22true%22);%0A        return properties;%0A    %7D%0A%0A
     @Test%0A  
@@ -872,10 +872,9 @@
 t = 
-10
+5
 0;%0A 
@@ -983,32 +983,33 @@
 saction = null;%0A
+%0A
         try %7B%0A  
@@ -1247,130 +1247,21 @@
     
-Post post = new Post( String.format( %22Post %25d%22, i ) );%0A                entityManager.persist( post );%0A                if (
+if ( i %3E 0 &&
  i %25
@@ -1446,24 +1446,24 @@
 on.begin();%0A
-
             
@@ -1459,32 +1459,155 @@
                %7D
+%0A%0A                Post post = new Post( String.format( %22Post %25d%22, i + 1 ) );%0A                entityManager.persist( post );
 %0A            %7D%0A%0A

Improve batch processing example