From 955b587eb7923ce3babacb1b7de7981ddbc95652 Mon Sep 17 00:00:00 2001 From: Jan Schatteman Date: Mon, 4 Mar 2024 23:15:44 +0100 Subject: [PATCH] HHH-17776 - Fix whitespace padding issues for literals in select expressions on Derby and HSQLDB Signed-off-by: Jan Schatteman --- .../java/org/hibernate/dialect/DerbySqlAstTranslator.java | 7 ++++++- .../java/org/hibernate/dialect/HSQLSqlAstTranslator.java | 7 ++++++- .../hibernate/sql/ast/spi/AbstractSqlAstTranslator.java | 2 +- .../orm/test/jpa/criteria/subquery/SubqueryTests.java | 4 ++-- 4 files changed, 15 insertions(+), 5 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/DerbySqlAstTranslator.java b/hibernate-core/src/main/java/org/hibernate/dialect/DerbySqlAstTranslator.java index 9dec7ba11d..cca3841c41 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/DerbySqlAstTranslator.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/DerbySqlAstTranslator.java @@ -232,7 +232,12 @@ public class DerbySqlAstTranslator extends AbstractSqlA @Override protected void renderSelectExpression(Expression expression) { - renderSelectExpressionWithCastedOrInlinedPlainParameters( expression ); + if ( isInSubquery() && expression instanceof Literal ) { + renderCasted( expression ); + } + else { + renderSelectExpressionWithCastedOrInlinedPlainParameters( expression ); + } } @Override diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/HSQLSqlAstTranslator.java b/hibernate-core/src/main/java/org/hibernate/dialect/HSQLSqlAstTranslator.java index 235a88e7e1..691ffd7bdf 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/HSQLSqlAstTranslator.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/HSQLSqlAstTranslator.java @@ -258,7 +258,12 @@ public class HSQLSqlAstTranslator extends AbstractSqlAs @Override protected void renderSelectExpression(Expression expression) { - renderSelectExpressionWithCastedOrInlinedPlainParameters( expression ); + if ( isInSubquery() && expression instanceof Literal ) { + renderCasted( expression ); + } + else { + renderSelectExpressionWithCastedOrInlinedPlainParameters( expression ); + } } @Override diff --git a/hibernate-core/src/main/java/org/hibernate/sql/ast/spi/AbstractSqlAstTranslator.java b/hibernate-core/src/main/java/org/hibernate/sql/ast/spi/AbstractSqlAstTranslator.java index b870e64313..b32daaca84 100644 --- a/hibernate-core/src/main/java/org/hibernate/sql/ast/spi/AbstractSqlAstTranslator.java +++ b/hibernate-core/src/main/java/org/hibernate/sql/ast/spi/AbstractSqlAstTranslator.java @@ -2442,7 +2442,7 @@ public abstract class AbstractSqlAstTranslator implemen .get( 1 ) == getCurrentQueryPart(); } - private boolean isInSubquery() { + protected boolean isInSubquery() { return statementStack.depth() > 1 && statementStack.getCurrent() instanceof SelectStatement && !( (SelectStatement) statementStack.getCurrent() ).getQueryPart().isRoot(); } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/criteria/subquery/SubqueryTests.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/criteria/subquery/SubqueryTests.java index 334ee9aa61..0204e2fa14 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/criteria/subquery/SubqueryTests.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/criteria/subquery/SubqueryTests.java @@ -122,8 +122,8 @@ public class SubqueryTests { JpaDerivedRoot mainQueryRoot = mainQuery.from( unionAllSubQuery ); mainQuery.multiselect( - builder.trim(mainQueryRoot.get("name")).alias("name"), - builder.trim(mainQueryRoot.get("surName")).alias("surName") + mainQueryRoot.get("name").alias("name"), + mainQueryRoot.get("surName").alias("surName") ); mainQuery.orderBy( builder.asc(mainQueryRoot.get("name")) );