title: Spring Data JPA Multiple Optional Search Parameters With Multiple Joins
body: <p>I'm currently using Spring Boot with Spring Data JPA to connect to an oracle database. With one parameter I just use the Spring Repository <code>findById(Long id);</code>  and it works great. Search on the other hand is much more complicated for me. In our case, the user provides a nested JSON object with multiple optional search parameters (at this point I can't change the way the way they send their data it has to be through the nested JSON). Here is what the JSON input object looks like:</p>
  
  <pre><code>{
    "agent_filter": {
      "first_name": "string",
      "last_name": "string",
      "agentNumber": "string",
      "agentCode": "string"
    },
    "account": "string",
    "status": "string",
    "paid": "string",
    "amount": "string",
    "person_filter": {
      "date_of_birth": "string",
      "first_name": "string",
      "last_name": "string",
      "tax_id": "string"
    }
  }
  </code></pre>
  
  <p>All the search criteria are optional (except at least 1 parameter) </p>
  
  <p>On the back-end we have the following entities:</p>
  
  <pre><code>@Entity
  Account{
  @OneToMany
  List&lt;PersonRole&gt; role;
  
  @OneToMany
  List&lt;AgentRole&gt; role;
  }
  
  @Entity
  PersonRole{
  String role;
  
  Person person;
  }
  
  @Entity
  AgentRole{
  String role;
  
  Agent agent;
  }
  
  @Entity
  Person{...}
  
  @Entity
  Agent{...}
  </code></pre>
  
  <p>So to provide the search functionality I can do multiple joins. I started using JPQL with an @Query notation but I had to do <code>is null or</code> checks with each parameter and it's a big mess. I started looking into other options and I saw stuff about QueryDSL, criteria, specification but I wasn't sure which one I should focus on and learn about. Unfortunately I don't know a whole lot on this subject and I was hoping someone can point me in the right direction for a good implementation of this search. Thank you!</p>