Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
import com.querydsl.core.types.ParamExpression;
import com.querydsl.core.types.Path;
import com.querydsl.core.types.Predicate;
import com.querydsl.core.types.Projections;
import com.querydsl.core.types.SubQueryExpression;
import com.querydsl.core.types.TemplateExpression;
import com.querydsl.core.types.Visitor;
Expand Down Expand Up @@ -260,14 +261,14 @@ public Object visit(SubQueryExpression<?> subQuery, Object criteriaBuilder) {
renderGroupBy(subQueryMetadata, (GroupByBuilder<?>) criteriaBuilder);
renderHaving(subQueryMetadata, (HavingBuilder<?>) criteriaBuilder);

Expression<?> select = subQueryMetadata.getProjection();
Expression<?> select = extractExpression(subQueryMetadata);
if (select instanceof FactoryExpression<?> && criteriaBuilder instanceof FullQueryBuilder<?, ?>) {
FactoryExpression<T> factoryExpression = (FactoryExpression<T>) select;
FullQueryBuilder<?, ?> fullQueryBuilder = (FullQueryBuilder<?, ?>) criteriaBuilder;
criteriaBuilder = fullQueryBuilder.selectNew(new FactoryExpressionObjectBuilder(factoryExpression));

} else {
List<? extends Expression<?>> projection = expandProjection(subQueryMetadata.getProjection());
List<? extends Expression<?>> projection = expandProjection(extractExpression(subQueryMetadata));

if (criteriaBuilder instanceof SelectBaseCTECriteriaBuilder) {
SelectBaseCTECriteriaBuilder<?> selectBaseCriteriaBuilder = (SelectBaseCTECriteriaBuilder<?>) criteriaBuilder;
Expand Down Expand Up @@ -905,6 +906,27 @@ private static String relativePathString(Path<?> root, Path<?> path) {
return pathString.toString();
}

private static Expression<?> extractExpression(QueryMetadata meta) {
Expression<?> projection = meta.getProjection();
if (projection != null) {
return projection;
}

List<JoinExpression> joins = meta.getJoins();
if (joins == null || joins.isEmpty()) {
return null;
}

JoinExpression firstJoin = joins.get(0);
if (firstJoin == null) {
return null;
}

return firstJoin.getType() == com.querydsl.core.JoinType.DEFAULT
? Projections.constructor(firstJoin.getTarget().getType())
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a convenience (desugar) or query language feature? The PR is suggested as a fix but what states that subqueries without a projection were ever allowed in JPQL/JPQL Next?

Doing this kind of expression manipulation makes a few assumptions that may not always be true:

  • The entity is a class
  • The entity name is the classname
  • The entity name is the alias for the select expression

Please add a test case to https://github.com/Blazebit/blaze-persistence/blob/main/integration/querydsl/testsuite/src/test/java/com/blazebit/persistence/querydsl/BasicQueryTest.java so we can have a look that the desired query behaviour.

: null;
}

/**
* Visitor that parses {@link JPQLNextOps#WITH_ALIAS}.
*/
Expand Down Expand Up @@ -1523,7 +1545,7 @@ public Object visit(SubQueryExpression<?> expr, Object context) {

renderCTEs(metadata);

metadata.getProjection().accept(this, context);
extractExpression(metadata).accept(this, context);

for (QueryFlag flag : metadata.getFlags()) {
flag.getFlag().accept(this, context);
Expand Down