I have a spring @Transactional
read-only service that spans several calls to a JpaRepository
with a method using a native query that returns a boolean
. The query is heavy and when checking logs looks like is executed several times with same parameters.
Shouldn't be only called once? Does first-level cache not working when using Spring Transactions? is it because it's returning a boolean
instead of an @Entity
?
First level cache works only for results which EntityManagers can manage, which are Entities. (or Session within Hibernate)
If you would like to cache other objects or simple types I'd suggest to use @Cacheable annotation with some cache provider, for example EhCache LINK
EDIT:
Your own queries aren't part of first level cache mechanism. Only simple operation like, persist, merge, update etc. can use it. For more information check this article LINK