When does a query hit JPA 1st level cache and when the query does bypass the cache

1.9k Views Asked by At

It is clear that, by default, when we execute entityManager.find(Post.class, 1L); then the Post instance for id=1 will be retrieved and placed in 1st level cache and then if we again execute the same entityManager.find(Post.class, 1L); in the same transaction, the post instance will be returned directly from 1st level cache without querying the database.

My question is the following:

Is the 1st level cache checked only when we try to get the entity by id executing entityManager.find(...); method ? I mean what if we fetch the same Post instance(having id = 1) by different property other than ID, e.g. using Criteria query fetching the post by name ? Is it still going to check the 1st level cache ?

What about querying the same row by native query, jpql or spring data query method ? Does JPA/Hibernate parse native query and jpql queries in order to find out if there is a corresponding entity in 1st level cache ?

0

There are 0 best solutions below