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

import org.hibernate.FlushMode;
import org.hibernate.Session;
import org.junit.Ignore;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

/**
 * @author Vlad Mihalcea
 */
public class HibernateAutoFlushTest extends JPAAutoFlushTest {

    @Override
    protected boolean nativeHibernateSessionFactoryBootstrap() {
        return true;
    }

    @Test
    @Ignore
    public void testFlushAutoNativeSQL() {
        doInJPA(entityManager -> {
            assertEquals(
                0,
                ((Number)
                    entityManager
                    .createNativeQuery(
                        "select count(*) " +
                        "from post")
                    .getSingleResult()
                ).intValue()
            );

            entityManager.persist(
                new Post()
                .setTitle("High-Performance Java Persistence")
            );

            int postCount = ((Number)
            entityManager
            .createNativeQuery(
                "select count(*) " +
                "from post")
            .getSingleResult()).intValue();

            assertEquals(1, postCount);
        });
    }

    @Test
    public void testFlushAutoNativeSQLFlushModeAlways() {
        doInJPA(entityManager -> {
            assertEquals(
                0,
                ((Number)
                    entityManager
                    .createNativeQuery(
                        "select count(*) " +
                        "from post")
                    .getSingleResult()
                ).intValue()
            );

            entityManager.persist(
                new Post()
                .setTitle("High-Performance Java Persistence")
            );

            int postCount = ((Number)
            entityManager
            .unwrap(Session.class)
            .createNativeQuery(
                "select count(*) " +
                "from post")
            .setFlushMode(FlushMode.ALWAYS)
            .getSingleResult()).intValue();

            assertEquals(1, postCount);
        });
    }

    @Test
    public void testFlushAutoNativeSQLSynchronizedEntityClass() {
        doInJPA(entityManager -> {
            assertEquals(
                0,
                ((Number)
                    entityManager
                    .createNativeQuery(
                        "select count(*) " +
                        "from post")
                    .getSingleResult()
                ).intValue()
            );

            entityManager.persist(
                new Post()
                .setTitle("High-Performance Java Persistence")
            );

            int postCount = ((Number)
            entityManager
            .unwrap(Session.class)
            .createNativeQuery(
                "select count(*) " +
                "from post")
            .addSynchronizedEntityClass(Post.class)
            .getSingleResult()).intValue();

            assertEquals(1, postCount);
        });
    }

    @Test
    public void testFlushAutoNativeSQLSynchronizedQuerySpace() {
        doInJPA(entityManager -> {
            assertEquals(
                0,
                ((Number)
                    entityManager
                    .createNativeQuery(
                        "select count(*) " +
                        "from post")
                    .getSingleResult()
                ).intValue()
            );

            entityManager.persist(
                new Post()
                .setTitle("High-Performance Java Persistence")
            );

            int postCount = ((Number)
            entityManager
            .unwrap(Session.class)
            .createNativeQuery(
                "select count(*) " +
                "from post")
            .addSynchronizedQuerySpace("post")
            .getSingleResult()).intValue();

            assertEquals(1, postCount);
        });
    }
}

@@ -111,16 +111,50 @@
 ession;%0A
+import org.hibernate.query.Query;%0A
 import o
@@ -458,20 +458,8 @@
 est%0A
-    @Ignore%0A
     
@@ -992,39 +992,484 @@
             
-int postCount =
+assertEquals(%0A                0,%0A                ((Number)%0A                entityManager%0A                .createNativeQuery(%0A                    %22select count(*) %22 +%0A                    %22from post%22)%0A                .getSingleResult()%0A                ).intValue()%0A            );%0A        %7D);%0A    %7D%0A%0A    @Test%0A    public void testFlushAutoNativeSQLFlushModeAlways() %7B%0A        doInJPA(entityManager -%3E %7B%0A            assertEquals(%0A                0,%0A               
  ((Number)%0A 
@@ -1471,32 +1471,40 @@
 er)%0A            
+        
 entityManager%0A  
@@ -1493,32 +1493,40 @@
   entityManager%0A
+        
             .cre
@@ -1549,32 +1549,40 @@
                 
+ 
+       
 %22select count(*)
@@ -1594,32 +1594,40 @@
                 
+        
 %22from post%22)%0A   
@@ -1627,32 +1627,40 @@
 t%22)%0A            
+  
+      
 .getSingleResult
@@ -1665,60 +1665,645 @@
 lt()
-).intValue();%0A%0A            assertEquals(1, postCount
+%0A                ).intValue()%0A            );%0A%0A            entityManager.persist(%0A                new Post()%0A                .setTitle(%22High-Performance Java Persistence%22)%0A            );%0A%0A            assertEquals(%0A                1,%0A                ((Number)%0A                    entityManager%0A                    .createNativeQuery(%0A                        %22select count(*) %22 +%0A                        %22from post%22%0A                    )%0A                    .unwrap(org.hibernate.query.Query.class)%0A                    .setHibernateFlushMode(FlushMode.ALWAYS)%0A                    .getSingleResult()%0A                ).intValue()%0A            
 );%0A 
@@ -2350,39 +2350,23 @@
 oid test
-FlushAutoNativeSQLFlush
+Session
 ModeAlwa
@@ -2863,46 +2863,8 @@
 );%0A%0A
-            int postCount = ((Number)%0A
     
@@ -2912,32 +2912,200 @@
 (Session.class)%0A
+            .setHibernateFlushMode(FlushMode.ALWAYS);%0A%0A            assertEquals(%0A                1,%0A                ((Number)%0A                    entityManager%0A        
             .cre
@@ -3112,32 +3112,40 @@
 ateNativeQuery(%0A
+        
                 
@@ -3173,32 +3173,40 @@
                 
+      
+  
 %22from post%22)%0A   
@@ -3214,44 +3214,8 @@
     
-    .setFlushMode(FlushMode.ALWAYS)%0A
     
@@ -3232,32 +3232,49 @@
 etSingleResult()
+%0A                
 ).intValue();%0A%0A 
@@ -3261,34 +3261,32 @@
     ).intValue()
-;%0A
 %0A            ass
@@ -3286,33 +3286,8 @@
     
-assertEquals(1, postCount
 );%0A 

Add more Hibernate ALWAYS flush mode examples