We're updating the issue view to help you get more done. 

CriteriaQuery with correlated subquery can't be reused after execution

Description

Spec 6.8

1 A CriteriaQuery, CriteriaUpdate, or CriteriaDelete object may be modified, either before or after Query or TypedQuery objects have been created and executed from it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 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);

It looks like executing a CriteriaQuery stores some resolved alias for entity types in the CriteriaQuery instance.

The correct subquery:

1 2 3 4 5 6 7 8 9 where ( select count(item1_.id) from Item item1_ where item0_.name=? and item0_.id=item1_.id )=1

The broken subquery:

1 2 3 4 5 6 7 8 9 where ( select count(item1_.id) from Item item1_ where item1_.name=? and item1_.id=item1_.id )=1

Environment

None

Status

Assignee

Unassigned

Reporter

Christian Bauer

Fix versions

None

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Affects versions

4.3.6

Priority

Major