21

I'm using Spring Data JPA 1.7.1

I was trying pass query hints (e.g. for Hibernate query caching) to queries when using the querydsl methods of the standard Spring Data repositories, as defined in the QueryDslPredicateExecutor interface, e.g. findOne(Predicate predicate), findAll(Predicate predicate) etc.

I figured that they can be set by overriding the methods in QueryDslPredicateExecutor (in my repository interface or an intermediate interface) and adding the QueryHints annotation, but since I cannot find this documented anywhere I was wondering if this is the recommended way of doing it.

Here is an example:

public interface MyEntityRepository extends CrudRepository<MyEntity, Integer>, CacheableQueryDslPredicateExecutor<MyEntity> {

    @QueryHints(value = {
            @QueryHint(name = "org.hibernate.cacheable", value = "true"),
            @QueryHint(name = "org.hibernate.cacheMode", value = "NORMAL"),
            @QueryHint(name = "org.hibernate.cacheRegion", value = "myCacheRegion")
    })
    Iterable<T> findAll(Predicate predicate);

}
Nazaret K.
  • 3,409
  • 6
  • 22
  • 31
  • Are you sure about the version? Support for that was added via [DATAJPA-574](https://jira.spring.io/browse/DATAJPA-574) in 1.7.0.RC1. `QueryDslJpaRepository.createQuery(…)` should actually apply those hints. – Oliver Drotbohm Mar 11 '15 at 15:15
  • What I posted above actually works. I just wanted to know if it's the correct usage, because I can't find it documented anywhere, relating to querydsl. Thanks. – Nazaret K. Mar 11 '15 at 21:18

1 Answers1

11

This is the recommended and correct way of doing it, see documentation for 1.7.1:

http://docs.spring.io/spring-data/jpa/docs/1.7.1.RELEASE/reference/html/#jpa.query-hints

Michael Simons
  • 4,640
  • 1
  • 27
  • 38
  • 6
    Yes I have seen that, but that only mentions custom queries, not related to querydsl. My question was about querydsl queries. I have assumed it can be used as I posted (by overriding the standard methods). – Nazaret K. Mar 11 '15 at 21:19