JpaSpecificationExecutor JOIN + ORDER BY in Specification
<p>I have a query using a JOIN and ORDER BY and want to use it within my repository using the Criteria Api.</p>

<p>Here I found, how to wrap such a query into a CriteriaQuery (<a href="http://docs.oracle.com/javaee/6/tutorial/doc/gjivm.html" rel="noreferrer">Link</a>).</p>

<pre><code>CriteriaQuery&lt;Pet&gt; cq = cb.createQuery(Pet.class);
Root&lt;Pet&gt; pet = cq.from(Pet.class);
Join&lt;Pet, Owner&gt; owner = cq.join(Pet_.owners);
cq.select(pet);
cq.orderBy(cb.asc(owner.get(Owner_.lastName),owner.get(Owner_.firstName)));
</code></pre>

<p>On the other side, I found some examples to use the Criteria Api in Combination with a JpaRepository (<a href="http://www.cubrid.org/wiki_ngrinder/entry/how-to-create-dynamic-queries-in-springdata" rel="noreferrer">example</a>).</p>

<p>The Problem is that all methods in the repository expect a Specification:</p>

<pre><code>T findOne(Specification&lt;T&gt; spec);
</code></pre>

<p>which is always build like this:</p>

<pre><code>public static Specification&lt;PerfTest&gt; statusSetEqual(final Status... statuses) {
    return new Specification&lt;PerfTest&gt;() {
        @Override

        public Predicate toPredicate(Root&lt;PerfTest&gt; root, CriteriaQuery&lt;?&gt; query, CriteriaBuilder cb) {
            return cb.not(root.get("status").in((Object[]) statuses));
        }
    };

}
</code></pre>

<p>So at one side I know how to create a CriteriaQuery, and on the other side I need a Specification which is build from a Predicate, and I can not figure out how to parse the CriteriaQuery into a Specification/Predicate.</p>