Skip to content

Latest commit

 

History

History
50 lines (33 loc) · 1.96 KB

File metadata and controls

50 lines (33 loc) · 1.96 KB

What is @QueryHints in Spring Data JPA?

  • @QueryHints allows you to pass hints (special instructions or options) to the JPA provider (like Hibernate) when executing a query.
  • These hints can tweak how the query is executed or how the results are handled.
  • They are passed as javax.persistence.QueryHint annotations inside @QueryHints.

In your example:

@QueryHints(value = { @QueryHint(name = "name", value = "value") }, forCounting = false)
Page<User> findByLastname(String lastname, Pageable pageable);
  • value = { @QueryHint(name = "name", value = "value") } — Here, you specify a hint with a name and a value.
  • forCounting = false — This means the hints apply to the main query (the one fetching data), not to the count query that Spring Data JPA uses internally to calculate total results for pagination.

Why use QueryHints?

  • To control query behavior at a low level.

  • Examples of common query hints:

    • "org.hibernate.cacheable" = "true" — Enables Hibernate query caching.
    • "javax.persistence.fetchgraph" — To specify an entity graph for fetching associations.
    • "org.hibernate.readOnly" = "true" — Optimize query for read-only entities.
    • "javax.persistence.lock.timeout" — Set lock timeout.

Example of a practical QueryHint:

If you want to enable Hibernate query caching:

@QueryHints(value = { @QueryHint(name = "org.hibernate.cacheable", value = "true") }, forCounting = false)
Page<User> findByLastname(String lastname, Pageable pageable);

This tells Hibernate to cache the results of this query, improving performance if the same query is executed multiple times.


Summary

  • @QueryHints passes special instructions to JPA provider for query execution.
  • It can improve performance, change locking behavior, specify fetch strategies, etc.
  • forCounting = false means the hints apply only to the main select query, not the count query used for pagination.