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

import java.util.Properties;

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

    @Override
    protected Properties properties() {
        Properties properties = super.properties();
        properties.put("hibernate.jdbc.batch_size", "25");
        properties.put("hibernate.order_inserts", "true");
        properties.put("hibernate.order_updates", "true");
        properties.put("hibernate.jdbc.batch_versioned_data", "true");
        return properties;
    }

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

        EntityManager entityManager = entityManagerFactory().createEntityManager();;

        try {
            entityManager.getTransaction().begin();

            for ( int i = 0; i < entityCount; ++i ) {
                if ( i > 0 && i % batchSize == 0 ) {
                    flush( entityManager );
                }

                Post post = new Post( String.format( "Post %d", i + 1 ) );
                entityManager.persist( post );
            }

            entityManager.getTransaction().commit();
        } catch (RuntimeException e) {
            if ( entityManager.getTransaction().isActive()) {
                entityManager.getTransaction().rollback();
            }
            throw e;
        } finally {
            entityManager.close();
        }
    }

    private void flush(EntityManager entityManager) {
        entityManager.flush();
        entityManager.clear();

        entityManager.getTransaction().commit();
        entityManager.getTransaction().begin();
    }

    @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;
        }
    }
}

@@ -53,38 +53,8 @@
 g;%0A%0A
-import java.util.Properties;%0A%0A
 impo
@@ -153,16 +153,45 @@
 tence.*;
+%0Aimport java.util.Properties;
 %0A%0A/**%0A *
@@ -355,23 +355,18 @@
 ass%3C?%3E%5B%5D
- 
 %7B%0A
-    
         
@@ -816,31 +816,32 @@
 oid test
-BatchProcessing
+FlushClearCommit
 () %7B%0A   
@@ -979,17 +979,16 @@
 nager();
-;
 %0A%0A      
@@ -1065,17 +1065,16 @@
    for (
- 
 int i = 
@@ -1096,17 +1096,16 @@
 unt; ++i
- 
 ) %7B%0A    
@@ -1120,17 +1120,16 @@
     if (
- 
 i %3E 0 &&
@@ -1147,17 +1147,16 @@
 ize == 0
- 
 ) %7B%0A    
@@ -1177,17 +1177,16 @@
   flush(
- 
 entityMa
@@ -1190,17 +1190,16 @@
 yManager
- 
 );%0A     
@@ -1249,17 +1249,16 @@
 ew Post(
- 
 String.f
@@ -1263,17 +1263,16 @@
 .format(
- 
 %22Post %25d
@@ -1283,11 +1283,9 @@
  + 1
- ) 
+)
 );%0A 
@@ -1321,22 +1321,20 @@
 persist(
- 
 post
- 
 );%0A     
@@ -1451,17 +1451,16 @@
     if (
- 
 entityMa

Update test