title: how to write a single jpa specification for multiple entities
body: <p>I am working on a Spring Boot - App that has multiple entities having  some identical columns for filtering.</p>
  <p>Currently, I have the same query defined in multiple repositories, so after doing some research, I've stumbled across an article about JPA - Specifications: <a href="https://spring.io/blog/2011/04/26/advanced-spring-data-jpa-specifications-and-querydsl/" rel="nofollow noreferrer">https://spring.io/blog/2011/04/26/advanced-spring-data-jpa-specifications-and-querydsl/</a></p>
  <p>So I made a generic class to build JPA-Specifications:</p>
  <pre><code>public final class GenericSpecifications&lt;T&gt;
  {
      public Specification whereNameLikeAndDateGreatherThan(String fieldName, String fieldDate, String name, LocalDate date)
      {
         return (root, query, builder) -&gt; builder.lessThan(root.get(columnName), date);
      }
  }
  </code></pre>
  <p>So in the service I can use:</p>
  <pre><code>repository.findAll(whereNameLikeAndDateGreatherThan(Person_.name, Person_.date, &quot;Max&quot;, LocalDate.now());
  </code></pre>
  <p>In this way, I have one query/specification in a central place and I don't need to write/maintain the same query on all repositories.</p>
  <p>However, I have more complex queries, where I need to filter over multiple columns.
  This means that my methods, in my GenericSpecification-Class, become too bloated, since I need to pass multiple column names and the search-values, so I could end up with methods with 6 or more parameters.</p>
  <p>I could define an Abstract-Entity class extended by all other entities.This abstract entity would have all the common fields in order to be sure that all the entities have the same columns.
  Then I can use these names for filtering, so I don't have to pass the field/coulmn-names at all.</p>
  <p>But, I am not sure if this is the cleanest approach to my problem.
  Do you know if there is a better way to do this?</p>