As per the the JPA2 documentation, TypedQuery.setHint() accepts a string key of "javax.persistence.cache.retrieveMode" and enum value from CacheRetrieveMode. CacheRetrieveMode.BYPASS is specified to: "Bypass the cache: get data directly from the database." This is a standard JPA hint, required to be supported by all JPA providers.
When using this hint to attempt to bypass the cache with Hibernate as JPA provider, it is effectively ignored; stale data is returned, rather than current the data in the database.
A brief and uninformed inspection of the code indicates this is likely not JPA-specific, but affects Hibernate Core when using CacheMode, as well.
The attached testcase contains a small Gradle project with a single JUnit test demonstrating the behavior. The test is self-contained and uses "raw" JDBC to manipulate the database around the work done with the EntityManager. The problem is exhibited on all 3.6.* versions released to date; I have not tried older versions, nor the 4.* line.
|