CriteriaQuery criteria = cb.createQuery();
Root<Item> i = criteria.from(Item.class);
criteria.select(i).where(
cb.equal(i.get(Item_.name), "Foo")
);
/* THIS BREAKS THE QUERY
TypedQuery<Item> q = em.createQuery(criteria);
assertEquals(q.getResultList().size(), 1);
*/
Subquery<Long> subquery = criteria.subquery(Long.class);
Root<Item> i2 = subquery.from(Item.class);
subquery.select(cb.count(i2));
subquery.where(cb.and(
cb.equal(i.get(Item_.name), "Foo"),
cb.equal(i, i2)
)
);
criteria.where(cb.equal(subquery, 1));
TypedQuery<Item> again = em.createQuery(criteria);
assertEquals(again.getResultList().size(), 1);