title: Spring @QuerydslPredicate and QuerydslBinderCustomizer: is it possible to infuse default criteria into predicate generated from request params?
body: <p>I am using <strong>Spring Data JPA</strong> and <strong>QueryDsl</strong> (v.4.2.2), Java 8. I can explicitly construct search predicates and pass them to the repository methods. However, I like the idea of using the <code>@QuerydslPredicate</code> annotation on a web/REST controller's method argument when the queried entities have more than a few properties, and I want the flexibility of filtering the search by any of them. So, something like this, generally, works very well:</p>
  <pre class="lang-java prettyprint-override"><code>    @GetMapping(&quot;/accounts/summaries&quot;)
      public PageDto&lt;AccountSummaryDto&gt; getAccountSummaries(@QuerydslPredicate(root = AccountSummary.class) Predicate accountSearchPredicate,
              @RequestParam(name = &quot;pageIndex&quot;, defaultValue = &quot;0&quot;) int pageIndex,
              @RequestParam(name = &quot;pageSize&quot;, defaultValue = &quot;25&quot;) int pageSize,
              @RequestParam(name = &quot;sortBy&quot;, defaultValue = &quot;id&quot;) String sortBy,
              @RequestParam(name = &quot;sortOrder&quot;, defaultValue = &quot;desc&quot;) String sortOrder) {
  
           // delegating to web-agnostic service that:
           // - creates Pageable pageRequest,     
           // - calls accountSummaryRepository.findAll(predicate, pageRequest), 
           // - constructs custom PageDto wrapper, etc. 
           return accountService.retrieveAccountSummaries(accountSearchPredicate, pageIndex, pageSize, sortBy, sortOrder); 
      }
  </code></pre>
  <p>My Spring Data JPA repository interface looks similar to this:</p>
  <pre class="lang-java prettyprint-override"><code>    public interface AccountSummarySearchRepository
          extends JpaRepository&lt;AccountSummary, Integer&gt;, QuerydslPredicateExecutor&lt;AccountSummary&gt;, QuerydslBinderCustomizer&lt;QAccountSummary &gt; {
  
      @Override
      default void customize(QuerydslBindings bindings, QAccountSummary acctSummary) {
          bindings.bind(acctSummary.customer.firstName).first((path, value) -&gt; path.isNull().or(path.startsWithIgnoreCase(value))) ;
          bindings.bind(acctSummary.customer.lastName).first((path, value) -&gt; path.isNull().or(path.startsWithIgnoreCase(value))) ;
          
          // etc.
  
          // default binding for String properties to be case insensitive &quot;contains&quot; match
          bindings.bind(String.class).first(
                  (StringPath path, String value) -&gt; path.isNull().or(path.containsIgnoreCase(value)));
      }
  </code></pre>
  <p><strong>My question:</strong></p>
  <blockquote>
  <p>The bindings in the <code>customize</code> method are set using the entity field
  paths and the <code>values</code> of the request parameters that match those
  paths. If the parameter is not specified, is there a way to bind the
  path to some constant value or a value obtained dynamically?</p>
  </blockquote>
  <p>For example, I want to always <em>ONLY</em> retrieve the entities where property <code>deleted</code> is set to <code>false</code> - without forcing the client to pass that as a query parameter? Similarly, I may want to set other default lookup values dynamically for each query. For example, I may want to &quot;retrieve only those accounts where <code>assignedTo</code> == <code>[current user ID available on a ThreadLocal]</code>...</p>
  <p>The following will not work</p>
  <pre><code>        bindings.bind(acctSummary.deleted).first((path, value) -&gt; path.eq(false));
  </code></pre>
  <p>because it, obviously, expects the <em>first occurrence of the path/value pair</em> for <code>deleted=...</code> in the Predicate (mapped from the incoming request params via the @QuerydslPredicate annotation. I don't want to pass that as a parameter because the requester does not even need to know about the existence of such field.</p>
  <p>Is there a simple way to infuse the Predicate instance that is auto-populated via the <code>@QuerydslPredicate</code> annotation with any additional implicit/default criteria that are not explicitly passed in the web request? Could this be done in the <code>customize</code> method? I suppose, one (very ugly) way would be to intercept the HTTP request in a filter - before it is processed by the Spring-QueryDsl framework - and replace it with a new request with added parameters? That would be a horrible solution, and I feel there has to be a better way to do it via some hook/capability provided by the framework itself.</p>
  <p>Unfortunately, there seem to be no comprehensive documentation for Spring QueryDsl support - other than some very simplistic examples.</p>
  <p>Thanks for your help!</p>