title: How to make a dynamic Spring (Boot) JPA query with paging?
body: <p>To oversimplify the problem: we have a class/table Wine (table &quot;wines&quot;) which has, among other attributes:</p>
  <ul>
  <li>name: String</li>
  <li>description: String</li>
  <li>origin: Origin</li>
  </ul>
  <p>... where Origin is another class (with table &quot;origins&quot;) with just <code>region: String</code> and <code>country: String</code>.</p>
  <p>What I'm trying to make is a search method in my repository for the RestController to use.</p>
  <p>The method declaration in the RestController is like this:</p>
  <pre><code>@GetMapping(&quot;/search&quot;)
  public Wine searchProduct(
          @RequestParam Optional&lt;String&gt; searchTerm,
          @RequestParam Optional&lt;Origin&gt; origin) {
      // ???
  }
  </code></pre>
  <p>What I'm trying to do now is the following: creating a query for the database where the searchTerm is used if given, same with origin. And it should be pageable.
  Example:</p>
  <pre><code>SELECT * FROM wines JOIN origins ON wines.origin_id = origins.id 
      WHERE (name LIKE $searchTerm OR description LIKE $searchTerm) AND (/*origin check*/)
  </code></pre>
  <p>If no search term is given, the whole &quot;() AND&quot; part should not be in the query. If no Origin is given... you get it.</p>
  <p>Things I've tried:</p>
  <ol>
  <li><p>(Naively) building a massive query in my repository (implements CrudRepository) like <a href="https://www.youtube.com/watch?v=i0Cto2IWybo" rel="noreferrer">here</a>.</p>
  <p><code>Page&lt;Wine&gt; findWinesByNameLikeOrDescriptionLikeAndOriginEquals(..., Pageable pageable);</code></p>
  <ul>
  <li>Which (apart from being super ugly, especially for more attributes) probably doesn't work because:
  <ol>
  <li>It's not defined whether OR or AND are more important (no brackets).</li>
  <li>No idea whether I can just shove an &quot;Origins&quot; object in there for it to work.</li>
  <li>No idea whether Pageable even works if that's a custom method.</li>
  </ol>
  </li>
  </ul>
  </li>
  <li><p>Using &quot;Specifications and Querydsl&quot; as suggested by spring.io <a href="https://spring.io/blog/2011/04/26/advanced-spring-data-jpa-specifications-and-querydsl" rel="noreferrer">here</a>.</p>
  <ul>
  <li>Which I'm just to stupid to understand, especially with those Q classes at the bottom of the post or the _ classes in the beginning. It just seems way too compicated for what it should do.</li>
  <li>Also there are no pagination options. There is, however, a possible fix like <a href="https://stackoverflow.com/questions/38777670/querydsl-paging-functionality">here</a> but I didn't get to it since using those _ and/or Q classes was to much of an overhead to start with.</li>
  </ul>
  </li>
  <li><p>Also <a href="https://stackoverflow.com/a/19714948/8710332">this</a> is just an example I found from 2013 which I don't even understand in general but kinda looks like it would be very fitting.</p>
  </li>
  </ol>