Skip to content

Commit 7e61364

Browse files
committed
HHH-13612 Quoted table name in FROM clause in @formula gets wrongly qualified with generated alias
1 parent f67da15 commit 7e61364

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

hibernate-core/src/main/java/org/hibernate/sql/Template.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,13 @@ public static String renderWhereStringTemplate(
167167

168168
boolean hasMore = tokens.hasMoreTokens();
169169
String nextToken = hasMore ? tokens.nextToken() : null;
170+
String token = null;
171+
String previousToken = null;
170172
while ( hasMore ) {
171-
String token = nextToken;
173+
if ( token != null ) {
174+
previousToken = token;
175+
}
176+
token = nextToken;
172177
String lcToken = token.toLowerCase(Locale.ROOT);
173178
hasMore = tokens.hasMoreTokens();
174179
nextToken = hasMore ? tokens.nextToken() : null;
@@ -203,7 +208,9 @@ else if ( quotedIdentifier && dialect.closeQuote()==token.charAt(0) ) {
203208
else {
204209
isOpenQuote = false;
205210
}
206-
if ( isOpenQuote ) {
211+
if ( isOpenQuote
212+
&& !inFromClause // don't want to append alias to tokens inside the from clause
213+
&& notEndsWithDot( previousToken ) ) {
207214
result.append( alias ).append( '.' );
208215
}
209216
}
@@ -232,7 +239,8 @@ else if ( FUNCTION_WITH_FROM_KEYWORDS.contains(lcToken) && "(".equals( nextToken
232239
result.append(token);
233240
inExtractOrTrim = true;
234241
}
235-
else if ( isIdentifier(token)
242+
else if ( !inFromClause // don't want to append alias to tokens inside the from clause
243+
&& isIdentifier( token )
236244
&& !isFunctionOrKeyword( lcToken, nextToken, dialect, typeConfiguration )
237245
&& !isLiteral( lcToken, nextToken, sql, symbols, tokens ) ) {
238246
result.append(alias)
@@ -268,6 +276,10 @@ else if ( inFromClause && ",".equals(lcToken) ) {
268276
return result.toString();
269277
}
270278

279+
private static boolean notEndsWithDot(String token) {
280+
return token == null || token != null && !token.endsWith( "." );
281+
}
282+
271283
private static boolean isLiteral(
272284
String lcToken, String next,
273285
String sqlWhereString, String symbols, StringTokenizer tokens) {

0 commit comments

Comments
 (0)