title: CriteriaBuilder: join one-to-many with ON clause
body: <p>Suppose you the following OneToMany relationships: <code>School-&gt;Student-&gt;ScientificWork</code>. Now you want to select all Schools that have Student with name 'John' and his scientific job is called 'Black Holes'. </p>
  
  <p>I do it like the following, but for some reason it retrurns me all possible schools.</p>
  
  <pre><code>public static Specification&lt;School&gt; spec() {
      return (root, query, cb) -&gt; {
          final SetJoin&lt;School, Student&gt; studs = root.joinSet("students", JoinType.LEFT);
          final SetJoin&lt;Student, ScientificWork&gt; works = root.joinSet("works", JoinType.LEFT);
          return cb.and(
                  cb.equal(studs.get(Student_.name), 'John'),
                  cb.equal(nodes.get(ScientificWork_.name), 'Black Holes')
          );
      };
  }
  </code></pre>
  
  <h2>Update</h2>
  
  <p>After finding <a href="https://stackoverflow.com/questions/15990141/how-to-make-a-criteriabuilder-join-with-a-custom-on-condition/37993919#37993919">this answer</a> I tried the following, but with the same result (it returns me all Schools instead of one):</p>
  
  <pre><code>public static Specification&lt;School&gt; spec() {
      return (root, query, cb) -&gt; {
          final SetJoin&lt;School, Student&gt; studs = root.joinSet("students", JoinType.LEFT);
          studs.on(cb.equal(studs.get(Student_.name), 'John'));
          final SetJoin&lt;Student, ScientificWork&gt; works = root.joinSet("works", JoinType.LEFT);          
          return cb.equal(nodes.get(ScientificWork_.name), 'Black Holes');
      };
  }
  </code></pre>