title: Generating correct and or condition with JPA Criteria API
body: <p>I am trying to generate query with multiple brackets in 'or' and 'and' conditions but internal brackets are not getting generated.
  Brackets for outer predicates are generating properly but not for internal ones.
  Code:</p>
  
  <pre><code>    CriteriaBuilder criteriaBuilder = this.getEntityManager().getCriteriaBuilder();
      CriteriaQuery&lt;ConfigurationKey&gt; query = criteriaBuilder.createQuery(ConfigurationKey.class);
      Root&lt;ConfigurationKey&gt; configurationKeyRoot = query.from(ConfigurationKey.class);
      Join&lt;ConfigurationKey, Customer&gt; configurationKeyCustomerJoin = configurationKeyRoot.join(ConfigurationKey_.customer);
  
      final List&lt;Predicate&gt; predicates = new ArrayList&lt;&gt;();
      predicates.add(criteriaBuilder.equal(configurationKeyCustomerJoin.get(Customer_.externalId), customerId));
      predicates.add(criteriaBuilder.equal(configurationKeyRoot.get(ConfigurationKey_.configType), configType));
  
      final List&lt;Predicate&gt; orPredicates = new ArrayList&lt;&gt;();
      keys.forEach((x, y) -&gt; {
                  orPredicates.add(
                          criteriaBuilder.and(
                                  criteriaBuilder.equal(configurationKeyRoot.get(ConfigurationKey_.keyType), x),
                                  criteriaBuilder.equal(configurationKeyRoot.get(ConfigurationKey_.keyValue), y)
                          )
                  );
              }
  
      );
  
      predicates.add(criteriaBuilder.or(orPredicates.toArray(new Predicate[orPredicates.size()])));
      query.where(criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()])));
      query.select(configurationKeyRoot);
      TypedQuery&lt;ConfigurationKey&gt; typedQuery = this.getEntityManager().createQuery(query);
  </code></pre>
  
  <p>Generated Query:</p>
  
  <pre><code>select
      configurat0_.id as id1_1_,
      configurat0_.createdAt as createdA2_1_,
      configurat0_.updatedAt as updatedA3_1_,
      configurat0_.configType as configTy4_1_,
      configurat0_.customerId as customer7_1_,
      configurat0_.keyType as keyType5_1_,
      configurat0_.keyValue as keyValue6_1_ 
  from
      configuration_keys configurat0_ 
  inner join
      customers customer1_ 
          on configurat0_.customerId=customer1_.id 
  where
      customer1_.externalId=? 
      and configurat0_.configType=? 
      and (
          configurat0_.keyType=? 
          and configurat0_.keyValue=? 
          or configurat0_.keyType=? 
          and configurat0_.keyValue=?
      )
  </code></pre>
  
  <p>Expected Query:</p>
  
  <pre><code>select
      configurat0_.id as id1_1_,
      configurat0_.createdAt as createdA2_1_,
      configurat0_.updatedAt as updatedA3_1_,
      configurat0_.configType as configTy4_1_,
      configurat0_.customerId as customer7_1_,
      configurat0_.keyType as keyType5_1_,
      configurat0_.keyValue as keyValue6_1_ 
  from
      configuration_keys configurat0_ 
  inner join
      customers customer1_ 
          on configurat0_.customerId=customer1_.id 
  where
      customer1_.externalId=? 
      and configurat0_.configType=? 
      and (
          (configurat0_.keyType=? 
          and configurat0_.keyValue=?) 
          or 
          (configurat0_.keyType=? 
          and configurat0_.keyValue=?)
      )
  </code></pre>
  
  <p>Please point any mistake.</p>