First of all, why would you go to the cache with an identifier value that doesn't exist?
Anyway, you can still get what you are asking for. All you need to do is to switch to a cacheable query:
Code:
doInJPA(entityManager -> {
Session session = entityManager.unwrap(Session.class);
List<Post> post = session
.createQuery("select p from Post p where p.id = :id")
.setParameter("id", 1L)
.setCacheable(true)
.list();
});
You need to enable query caching too:
Code:
hibernate.cache.use_query_cache = true
This way, the query result will be cached in the database and the second transaction will load the result from the query cache and there is no database roundtrip.