Objects in the second-level cache are retrieved only by id, so Hibernate will always run a request to get a list of identifiers, and then read these objects either from the second-level cache or using another request.
Hibernate, on the other hand, can cache the request and completely avoid calling the database in some situations. However, you should keep in mind that changing any of the tables participating in the query will invalidate it, so you may not often delete the cache. See here for a description of how the cache request works.
So the cost of your query is 0 if the query is cached or about the same as the query in direct SQL. Depending on how often your data changes, you can save a lot by turning on query caching or you wonβt be able to save anything.
If you have a large volume of queries, and you can tolerate outdated results, I would say that it is much better to use a different cache for query results that expire only every minute.
source share