Вопрос

We are using Hibernate in our project. My question is regarding named queries. Can someone guide me on how named queries are handled by Hibernate. If Hibernate is configured to use level 1 cache will it also return results for the named query from the cache or will it always make a trip to the database?

Это было полезно?

Решение

The level-1 cache is the Hibernate session. Except in very rare case where a stateless session is used, there is a level-1 cache by default. Queries results are never cached in the level-1 cache. I think you meant "second-level cache".

Named queries are handled exactly like unnamed-queries regarding the cache.

Queries can be cached or not. If they're cached, a round-trip to the database can be avoided. Whether they're cached or not, if they return instances of entities that are in the second-level cache, the query will return the IDs, and then the entities themselves will be loaded from the second-level cache.

See Hibernate 2nd level cache in a Grails app and the reference documentation for more information.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top