For most queries, including criteria queries, the query cache is not very efficient, because query cache invalidation occurs too frequently. However, there is one special kind of query where we can optimize the cache invalidation algorithm: lookups by a constant natural key. In some applications, this kind of query occurs frequently. The criteria API provides special provision for this use case.
First, you should map the natural key of your entity using
<natural-id>, and enable use of the second-level cache.
Note that this functionality is not intended for use with entities with mutable natural keys.
Next, enable the Hibernate query cache.
Restrictions.naturalId() allows us to make use of the more efficient cache algorithm.