Spring Data JPA and QueryDSL

java, querydsl, spring-data

Solution

Probably the most convenient way is to let your repository interfaces simply extend `QueryDslPredicateExecutor` which adds the capability to simply pipe Querydsl `Predicate` objects into the repository and execute them standalone or alongside `Pageable` and `Sort` and the like.

If you really want to hide the combination of predicates into the repository layer (which is absolutely fine but actually serves a different purpose) you create a separate repository implementation class as described here and use `QueryDslRepositorySupport` as base class. In your implemented finder methods you can then just use the `from(…)`, `update(…)` and `delete(…)` methods of the base class to easily construct and execute queries using the Querydsl meta-model.

Problem

I'm new to Spring data JPA and am trying to understand how to best use it with QueryDSL. Without QueryDSL, I would be able to simply create any queries in my SpringData interface with an @Query annotation. In order to have the same experience using QueryDSL, from what I can see, I need to either create my own custom repository implementation and have my repo interface extend my custom implementation interface or put all my QueryDSL queries at a service layer which wraps my repo. In the first case, I lose the ability to use any of the SD autogenerated methods (ex: findAll(QueryDSL predicate) ) in my custom repo since I don't have access to the actual repo object, and in the second case I am putting query logic at the service layer instead of at the repo layer. Neither solution sounds particularly attractive to me. Is there a 3rd way that is more appropriate? Or am I misunderstanding how to properly use QueryDSL and Spring Data? Thanks! Eric

Original source