What you enabled is the second-level cache. This cache caches the state of the entities, and indexes them by their ID. It's like a Map<ID, EntityState>
. This cache is only used if you get the entity by ID:
- by calling session.get()
- by calling session.load()
- by navigating through a XxxToOne association targeting the entity
Your query doesn't fall into this category: it looks for your entity by one of its fields, and not by its ID.
For the rest, Hibernate can't execute arbitrary SQL queries against the cache, and even if it could, the cache only contains a subset of the table, so it has to query the database.
You can however, cache the result of queries as well. You'll need to enable the query cache to do so, and to mark your query as cacheable. You can also cache an association (which seems like the reason for this query: it's looking for all the cities of a given state). You'll have to annotate the association with @Cache
.
Read the documentation.