Imagine having a tool that can automatically detect if you are using JPA and Hibernate properly.
Hypersistence Optimizer is that tool!
In this article, we are going to see how we can override the default FlushMode used by Hibernate.
If you bootstrap Hibernate natively, and not as a JPA provider, de default FlushMode.AUTO is used which, as explained in this article, does not trigger a Persistence Context flush prior to executing a native SQL query.
The FlushMode.ALWAYS mode instructs Hibernate to behave just like the JPA FlushModeType.AUTO mode, therefore, triggering a Persistence Context flush prior to any query, be it JPQL, Criteria API or a native SQL query.
If you want to override the FlushMode for every query executed by the current Hibernate Session, then you can change the FlushMode as follows:
Based on my book, High-Performance Java Persistence, this workshop teaches you various data access performance optimizations from JDBC, to JPA, Hibernate and jOOQ for the major rational database systems (e.g. Oracle, SQL Server, MySQL and PostgreSQL).