title: JPA Criteria Builder OneToMany Restrictions
body: <p>I have a Parent with a OneToMany associations with a Child Table.</p>
  
  <p>I'm trying to write a query with CriteriaBuilder to restrict the results returned from the Child table.</p>
  
  <p>I'm adding a Predicate, something like  </p>
  
  <pre><code>cb.equal(parent.get("children").get("sex"), "MALE")
  </code></pre>
  
  <p>If the Parent has a son or SON and Daughter it's returning that parent but also returning all the children they have.</p>
  
  <p>Hibernate fires off the first query with my predicates but the second query to get the children only uses the  JoinColumn in the where clause it doesn't include </p>
  
  <pre><code>cb.equal(parent.get("children").get("sex"), "MALE").
  </code></pre>
  
  <p>Thoughts?</p>
  
  <p>I am using a SetJoin </p>
  
  <pre><code>children = parent.joinSet("children", JoinType.LEFT)
  </code></pre>
  
  <p>CLARIFICATION:</p>
  
  <pre><code>public static Specification&lt;Parent&gt; findPlanBenefits(Integer parentId) {
      return (parent, query, cb) -&gt; {
          Predicate predicates = cb.conjunction();
          List&lt;Expression&lt;Boolean&gt;&gt; expressions = predicates.getExpressions();
  
          //Parent Criteria
          expressions.add(cb.equal(parent.get("parentId"), parentId));
  
          //Children Criteria
          SetJoin&lt;Parent, Children&gt; children = parent.joinSet("children", JoinType.LEFT);
  
          Predicate sex = cb.equal(children.get("sex"), "MALE");
          children.on(sex);
  
          return predicates;
      };
  }
  </code></pre>