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")) );