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

import org.hibernate.FlushMode;
import org.hibernate.cfg.AvailableSettings;
import org.junit.Test;

import java.util.Properties;

import static org.junit.Assert.assertEquals;

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

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

    @Override
    protected void additionalProperties(Properties properties) {
        properties.setProperty(
                AvailableSettings.FLUSH_MODE, FlushMode.ALWAYS.name()
        );
    }

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

@@ -886,36 +886,36 @@
             
-select count
+SELECT COUNT
 (*)%0A        
@@ -922,36 +922,36 @@
                 
-from
+FROM
  post%0A          
@@ -1288,20 +1288,20 @@
     
-select count
+SELECT COUNT
 (*)%0A
@@ -1320,12 +1320,12 @@
     
-from
+FROM
  pos

Use native SQL formatting