Stackoverflow when applying a restriction to the selection of a suquery

Description

I've got a (Criteria) sub-query which is generated somewhere else and where I want to add another restriction on the selection:

1 2 3 4 5 Subquery<MyEntity> subQuery = ....; subQuery.where(critieriaBuilder.and(subQuery.getRestriction(), critieriaBuilder.equal(subQuery.getSelection(), instance))); Predicate predicate = critieriaBuilder.exists(subQuery); ...

If I execute the associated query, I run into a StackOverflowException:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 ... at org.hibernate.jpa.criteria.CriteriaSubqueryImpl.render(CriteriaSubqueryImpl.java:266) at org.hibernate.jpa.criteria.CriteriaSubqueryImpl$SubquerySelection.render(CriteriaSubqueryImpl.java:137) at org.hibernate.jpa.criteria.predicate.ComparisonPredicate.render(ComparisonPredicate.java:170) at org.hibernate.jpa.criteria.predicate.AbstractSimplePredicate.render(AbstractSimplePredicate.java:48) at org.hibernate.jpa.criteria.predicate.CompoundPredicate.render(CompoundPredicate.java:171) at org.hibernate.jpa.criteria.predicate.CompoundPredicate.render(CompoundPredicate.java:115) at org.hibernate.jpa.criteria.predicate.CompoundPredicate.render(CompoundPredicate.java:105) at org.hibernate.jpa.criteria.QueryStructure.render(QueryStructure.java:246) at org.hibernate.jpa.criteria.CriteriaSubqueryImpl.render(CriteriaSubqueryImpl.java:266) at org.hibernate.jpa.criteria.CriteriaSubqueryImpl$SubquerySelection.render(CriteriaSubqueryImpl.java:137) at org.hibernate.jpa.criteria.predicate.ComparisonPredicate.render(ComparisonPredicate.java:170) at org.hibernate.jpa.criteria.predicate.AbstractSimplePredicate.render(AbstractSimplePredicate.java:48) at org.hibernate.jpa.criteria.predicate.CompoundPredicate.render(CompoundPredicate.java:171) at org.hibernate.jpa.criteria.predicate.CompoundPredicate.render(CompoundPredicate.java:115) at org.hibernate.jpa.criteria.predicate.CompoundPredicate.render(CompoundPredicate.java:105) at org.hibernate.jpa.criteria.QueryStructure.render(QueryStructure.java:246) at org.hibernate.jpa.criteria.CriteriaSubqueryImpl.render(CriteriaSubqueryImpl.java:266) ...

The problem is that the wrapper, which is returned by CriteriaSubqueryImpl.getSelection(), is not rendering the single expression, but the whole subquery instead.

This was introduced with HHH-7451, but I can't see where the JPA 2.1 spec explicitly requires this behavior. Maybe this is an obsolete leftover from the draft version?

Environment

None

Status

Assignee

Unassigned

Reporter

Tobias Liefke

Fix versions

None

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Affects versions

5.0.10
4.3.11
5.2.6

Priority

Major
Configure