QueryDSL – add subquery into FROM statement (2021)

In 2013 according to @Timo Westkämper (see QueryDSL – add subquery into FROM statement) it was possible to include a subquery in a FROM clause. Nowadays it seems this is not longer possible as JPQL specification does not allow it:

(https://docs.oracle.com/cd/E12839_01/apirefs.1111/e13946/ejb3_langref.html#ejb3_langref_subqueries)
Subqueries are restricted to the WHERE and HAVING clauses in this release. Support for subqueries in the FROM clause will be considered in a later release of the specification.

I don’t get how is this possible. Can you confirm it’s not possible to use subqueries in a FROM clause in querydsl 4.4.x?

Answer

If you read the linked question carefully, Timo is talking about “Querydsl SQL”. That’s the QueryDSL module that directly emits SQL, completely bypassing JPA.

That is, you can use a subquery in a from clause when using plain SQL, but not when using JPQL.

That’s why the method com.querydsl.sql.SQLCommonQuery.from has an overload that takes a SubQueryExpression, while the method com.querydsl.jpa.JPQLQuery.from doesn’t have such an overload.

Source: stackoverflow
The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .