title: Conditional where clause in JPA
body: <p>I have two tables "table1" and "table2". For each row in table1, there can be multiple rows in table2. To get the data, I have created following classes</p>
  
  <pre><code>@Entity
  @EntityListeners(AuditingEntityListener.class) 
  @Table(name = "table1", schema = "dbo")
  public class Table1 extends BaseDomain{
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer vendorId;
    private String name;
    private Integer companyId;
    private String details;
  
    @OneToMany(mappedBy = "table1",cascade = CascadeType.ALL)
    @NotFound(action = NotFoundAction.IGNORE)
    private Set&lt;Table2&gt; table2;
  
   // getter and setter for above
  }
  </code></pre>
  
  <p>and </p>
  
  <pre><code>    @Entity
      @EntityListeners(AuditingEntityListener.class) 
      @Table(name = "table2", schema = "dbo")
      public class Table2 extends BaseDomain{
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        private Integer id;
        private Integer cityId;
        private Integer companyId;
        @OneToOne
        @JoinColumn(name = "vendorId")
        @JsonIgnore
        private Table1 table1;
  
       // getter and setter for above
      }
  </code></pre>
  
  <p>Then I have created repository like this</p>
  
  <pre><code>@Transactional
  public interface Table1Repository  extends JpaRepository&lt;Table1, Integer&gt; {
  
    Page&lt;Table1&gt; findByCompanyId(Integer companyId,Pageable pageable);
  
  }
  </code></pre>
  
  <p>This will give me the list of rows from table1 as per companyId and list of rows with corresponding vendorId in Table2.</p>
  
  <p>Till now my implementation is working fine.</p>
  
  <p>The response is</p>
  
  <pre><code>{
    "content": [
      {
        "vendorId": 23,
        "name": "vendorname",
        "details": details,
        "table2": [
                   {
                    "id" :1,
                   "cityId":1,
                   },
                    "id" :2,
                    "cityId" : 3
                  ]
      },
     {....}
  ]
  }
  </code></pre>
  
  <p>Currently I am taking companyId as input and gives the above response. Now I want to take cityId as an input as well. So table2 output should contain the details of given cityId only.</p>
  
  <p>So can anyone help how can I achieve it? I don't want to create another model for table1 and table2.</p>