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

import com.vladmihalcea.book.hpjp.util.AbstractTest;
import com.vladmihalcea.book.hpjp.util.providers.Database;
import org.hibernate.Session;
import org.junit.Test;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import java.sql.BatchUpdateException;
import java.sql.PreparedStatement;
import java.util.Properties;
import java.util.concurrent.atomic.AtomicLong;

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

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

    @Override
    protected void additionalProperties(Properties properties) {
        properties.put("hibernate.jdbc.batch_size", "5");
        properties.put("hibernate.order_inserts", "true");
        properties.put("hibernate.order_updates", "true");
        properties.put("hibernate.jdbc.batch_versioned_data", "true");
    }

    @Override
    protected Database database() {
        return Database.MYSQL;
    }

    @Test
    public void test() {
        LOGGER.info("testInsertPosts");
        AtomicLong id = new AtomicLong();
        for (int it = 1; it <= 1000; it++) {
            doInJPA(entityManager -> {
                Session session = entityManager.unwrap(Session.class);
                session.doWork(connection -> {
                    try (PreparedStatement st = connection.prepareStatement("""
                            INSERT INTO post (id, title)
                            VALUES (?, ?)
                            """)) {
                        for (long i = 1; i <= 3; i++) {
                            st.setLong(1, id.incrementAndGet());
                            st.setString(2, String.format("High-Performance Java Persistence, Part %d", i));
                            st.addBatch();
                        }
                        st.executeBatch();
                    }
                });
                session.createQuery("select p from Post p").getResultList();
            });
            sleep(1000);
        }
    }

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

        @Id
        private Long id;

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

@@ -241,75 +241,9 @@
 nce.
-Entity;%0Aimport javax.persistence.Id;%0Aimport javax.persistence.Table
+*
 ;%0Aim
@@ -1109,99 +1109,8 @@
 %22);%0A
-        AtomicLong id = new AtomicLong();%0A        for (int it = 1; it %3C= 1000; it++) %7B%0A    
     
@@ -1152,20 +1152,16 @@
         
-    
 Session 
@@ -1211,36 +1211,32 @@
 s);%0A            
-    
 session.doWork(c
@@ -1246,28 +1246,24 @@
 ection -%3E %7B%0A
-    
             
@@ -1481,20 +1481,16 @@
         
-    
 for (lon
@@ -1517,77 +1517,8 @@
 ) %7B%0A
-                            st.setLong(1, id.incrementAndGet());%0A    
     
@@ -1642,20 +1642,16 @@
         
-   
- 
 st.addBa
@@ -1673,34 +1673,26 @@
             
-    %7D%0A    
+%7D%0A
             
@@ -1726,36 +1726,32 @@
                 
-    
 %7D%0A              
@@ -1744,36 +1744,28 @@
             
- 
-   
 %7D);%0A
-    
             
@@ -1833,32 +1833,170 @@
         
+%7D);%0A
     %7D
+%0A%0A    @Test%0A    public void testTransactionless() %7B%0A        EntityManager entityManager = entityManagerFactory().createEntityManager(
 );%0A
+%0A
         
     slee
@@ -1991,22 +1991,553 @@
     
-    sleep(1000
+for (long i = 1; i %3C= 3; i++) %7B%0A            entityManager.persist(%0A                new Post()%0A                    .setTitle(%0A                        String.format(%0A                            %22High-Performance Java Persistence, part %25d%22,%0A                            i%0A                        )%0A                    )%0A            );%0A        %7D%0A%0A        try %7B%0A            entityManager.getTransaction().begin();%0A            entityManager.getTransaction().commit();%0A        %7D catch (Exception e) %7B%0A            entityManager.getTransaction().rollback(
 );%0A 
@@ -2649,16 +2649,76 @@
     @Id%0A
+        @GeneratedValue(strategy = GenerationType.IDENTITY)%0A
         
@@ -2841,28 +2841,28 @@
      public 
-void
+Post
  setId(Long 
@@ -2889,24 +2889,49 @@
 is.id = id;%0A
+            return this;%0A
         %7D%0A%0A 
@@ -3016,20 +3016,20 @@
  public 
-void
+Post
  setTitl
@@ -3070,32 +3070,57 @@
 .title = title;%0A
+            return this;%0A
         %7D%0A    %7D%0A

Rework MySQL batch insert example