Skip to content

Commit 0ee13cf

Browse files
HHH-8301 - SQLServer2005LimitHandler skips column alias generation
(cherry picked from commit 6a71cbb)
1 parent 7e87c71 commit 0ee13cf

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

hibernate-core/src/main/java/org/hibernate/dialect/pagination/SQLServer2005LimitHandler.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,10 @@ protected String fillAliasInSelectClause(StringBuilder sb) {
154154
// Inserting alias. It is unlikely that we would have to add alias, but just in case.
155155
alias = StringHelper.generateAlias( "page", unique );
156156
sb.insert( nextComa, " as " + alias );
157+
int aliasExprLength = ( " as " + alias ).length();
157158
++unique;
158-
nextComa += ( " as " + alias ).length();
159+
nextComa += aliasExprLength;
160+
endPos += aliasExprLength;
159161
}
160162
aliases.add( alias );
161163
}

hibernate-core/src/test/java/org/hibernate/dialect/SQLServer2005DialectTestCase.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,19 @@ public void testGetLimitStringWithFromColumnName() {
8181
);
8282
}
8383

84+
@Test
85+
@TestForIssue(jiraKey = "HHH-8301")
86+
public void testGetLimitStringAliasGeneration() {
87+
final String notAliasedSQL = "select column1, column2, column3, column4 from table1";
88+
89+
assertEquals(
90+
"WITH query AS (SELECT inner_query.*, ROW_NUMBER() OVER (ORDER BY CURRENT_TIMESTAMP) as __hibernate_row_nr__ FROM ( " +
91+
"select column1 as page0_, column2 as page1_, column3 as page2_, column4 as page3_ from table1 ) inner_query ) " +
92+
"SELECT page0_, page1_, page2_, page3_ FROM query WHERE __hibernate_row_nr__ >= ? AND __hibernate_row_nr__ < ?",
93+
dialect.buildLimitHandler( notAliasedSQL, toRowSelection( 3, 5 ) ).getProcessedSql()
94+
);
95+
}
96+
8497
@Test
8598
@TestForIssue(jiraKey = "HHH-7019")
8699
public void testGetLimitStringWithSubselect() {

0 commit comments

Comments
 (0)