HHH-16647 allow unqualified use of query result type in HQL instantiation

This commit is contained in:
Gavin 2023-05-20 10:28:55 +02:00 committed by Gavin King
parent addd88000c
commit f673441ab3
2 changed files with 24 additions and 3 deletions

View File

@ -1306,11 +1306,13 @@ public class SemanticQueryBuilder<R> extends HqlParserBaseVisitor<Object> implem
final SqmDynamicInstantiation<?> dynamicInstantiation; final SqmDynamicInstantiation<?> dynamicInstantiation;
final ParseTree instantiationTarget = ctx.instantiationTarget().getChild( 0 ); final ParseTree instantiationTarget = ctx.instantiationTarget().getChild( 0 );
if ( instantiationTarget instanceof HqlParser.SimplePathContext ) { if ( instantiationTarget instanceof HqlParser.SimplePathContext ) {
final String className = instantiationTarget.getText(); String className = instantiationTarget.getText();
if ( expectedResultType!=null && expectedResultType.getSimpleName().equals( className ) ) {
className = expectedResultType.getName();
}
try { try {
final JavaType<?> jtd = resolveInstantiationTargetJtd( className );
dynamicInstantiation = SqmDynamicInstantiation.forClassInstantiation( dynamicInstantiation = SqmDynamicInstantiation.forClassInstantiation(
jtd, resolveInstantiationTargetJtd( className ),
creationContext.getNodeBuilder() creationContext.getNodeBuilder()
); );
} }

View File

@ -1920,4 +1920,23 @@ public class FunctionTests {
); );
} }
static class Pair {
int integer; double floating;
Pair(int integer, double floating) {
this.integer = integer;
this.floating = floating;
}
}
static class Triple {
int integer; double floating; String string;
}
@Test
public void testInstantiateLocalClass(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
session.createSelectionQuery("select new Pair(theInt, theDouble) from EntityOfBasics", Pair.class).getResultList();
session.createSelectionQuery("select new Triple(theInt as integer, theDouble as floating, 'hello' as string) from EntityOfBasics", Triple.class).getResultList();
}
);
}
} }