Skip to content

Commit 0899e49

Browse files
committed
HHH-18461 Fix native query doesn't respect query.setFirstResult(0)
Align to JPQL query, generated sql should contain `offset ? rows` and pass `0` as parameter.
1 parent 4456c88 commit 0899e49

File tree

9 files changed

+31
-28
lines changed

9 files changed

+31
-28
lines changed

hibernate-community-dialects/src/test/java/org/hibernate/community/dialect/AltibaseDialectTestCase.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public void testSupportLimits() {
4646
public void testSelectWithLimitOnly() {
4747
assertEquals( "select c1, c2 from t1 order by c1, c2 desc limit ?",
4848
withLimit("select c1, c2 from t1 order by c1, c2 desc",
49-
toRowSelection( 0, 15 ) ).toLowerCase( Locale.ROOT));
49+
toRowSelection( null, 15 ) ).toLowerCase( Locale.ROOT));
5050
}
5151

5252
@Test
@@ -67,7 +67,7 @@ private String withLimit(String sql, Limit limit) {
6767
return dialect.getLimitHandler().processSql( sql, -1, null, new LimitQueryOptions( limit ) );
6868
}
6969

70-
private Limit toRowSelection(int firstRow, int maxRows) {
70+
private Limit toRowSelection(Integer firstRow, Integer maxRows) {
7171
Limit selection = new Limit();
7272
selection.setFirstRow( firstRow );
7373
selection.setMaxRows( maxRows );

hibernate-community-dialects/src/test/java/org/hibernate/community/dialect/DerbyDialectTestCase.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public void testInsertLimitClause(SessionFactoryScope scope) {
4040
final String input = "select * from tablename t where t.cat = 5";
4141
final String expected = "select * from tablename t where t.cat = 5 fetch first ? rows only";
4242

43-
final String actual = withLimit( input, toRowSelection( 0, limit ) );
43+
final String actual = withLimit( input, toRowSelection( null, limit ) );
4444
assertEquals( expected, actual );
4545
}
4646

@@ -103,7 +103,7 @@ private String withLimit(String sql, Limit limit) {
103103
return new DerbyDialect().getLimitHandler().processSql( sql, -1, null, new LimitQueryOptions( limit ) );
104104
}
105105

106-
private Limit toRowSelection(int firstRow, int maxRows) {
106+
private Limit toRowSelection(Integer firstRow, Integer maxRows) {
107107
Limit selection = new Limit();
108108
selection.setFirstRow( firstRow );
109109
selection.setMaxRows( maxRows );

hibernate-community-dialects/src/test/java/org/hibernate/community/dialect/DerbyLegacyDialectTestCase.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public void testInsertLimitClause() {
2828
final String input = "select * from tablename t where t.cat = 5";
2929
final String expected = "select * from tablename t where t.cat = 5 fetch first " + limit + " rows only";
3030

31-
final String actual = withLimit( input, toRowSelection( 0, limit ) );
31+
final String actual = withLimit( input, toRowSelection( null, limit ) );
3232
assertEquals( expected, actual );
3333
}
3434

@@ -87,7 +87,7 @@ private String withLimit(String sql, Limit limit) {
8787
return new DerbyLegacyDialect( DatabaseVersion.make( 10, 5 ) ).getLimitHandler().processSql( sql, -1, null, new LimitQueryOptions( limit ) );
8888
}
8989

90-
private Limit toRowSelection(int firstRow, int maxRows) {
90+
private Limit toRowSelection(Integer firstRow, Integer maxRows) {
9191
Limit selection = new Limit();
9292
selection.setFirstRow( firstRow );
9393
selection.setMaxRows( maxRows );

hibernate-community-dialects/src/test/java/org/hibernate/community/dialect/FirebirdDialectTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ class FirebirdDialectTest {
1919
@ParameterizedTest
2020
@CsvSource(useHeadersInDisplayName = true, value = {
2121
"major, minor, offset, limit, expectedSQL",
22-
"2, 5, 0, 10, select first ? * from tablename t where t.cat = 5",
22+
"2, 5, , 10, select first ? * from tablename t where t.cat = 5",
2323
"2, 5, 10, 0, select skip ? * from tablename t where t.cat = 5",
2424
"2, 5, 5, 10, select first ? skip ? * from tablename t where t.cat = 5",
25-
"3, 0, 0, 10, select * from tablename t where t.cat = 5 fetch first ? rows only",
25+
"3, 0, , 10, select * from tablename t where t.cat = 5 fetch first ? rows only",
2626
"3, 0, 10, 0, select * from tablename t where t.cat = 5 offset ? rows",
2727
"3, 0, 5, 10, select * from tablename t where t.cat = 5 offset ? rows fetch next ? rows only"
2828
})
2929
@JiraKey( "HHH-18213" )
30-
void insertOffsetLimitClause(int major, int minor, int offset, int limit, String expectedSql) {
30+
void insertOffsetLimitClause(int major, int minor, Integer offset, Integer limit, String expectedSql) {
3131
String input = "select * from tablename t where t.cat = 5";
3232
FirebirdDialect dialect = new FirebirdDialect( DatabaseVersion.make( major, minor ) );
3333
String actual = dialect.getLimitHandler().processSql( input, -1, null, new LimitQueryOptions( new Limit( offset, limit ) ) );

hibernate-community-dialects/src/test/java/org/hibernate/community/dialect/SQLServer2005DialectTestCase.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ public void testGetLimitStringWithSelectDistinctSubselect() {
168168
"select top(?) col0_.CONTENTID as CONTENT1_12_ " +
169169
"where col0_.CONTENTTYPE='PAGE' and (col0_.CONTENTID in " +
170170
"(select distinct col2_.PREVVER from CONTENT col2_ where (col2_.PREVVER is not null)))",
171-
withLimit( selectDistinctSubselectSQL, toRowSelection( 0, 5 ) )
171+
withLimit( selectDistinctSubselectSQL, toRowSelection( null, 5 ) )
172172
);
173173
}
174174

@@ -254,7 +254,7 @@ public void testGetLimitStringWithMaxOnly() {
254254
assertEquals(
255255
"select top(?) product2x0_.id as id0_, product2x0_.description as descript2_0_ " +
256256
"from Product2 product2x0_ order by product2x0_.id",
257-
withLimit( query, toRowSelection( 0, 1 ) )
257+
withLimit( query, toRowSelection( null, 1 ) )
258258
);
259259

260260
final String distinctQuery = "select distinct product2x0_.id as id0_, product2x0_.description as descript2_0_ " +
@@ -263,7 +263,7 @@ public void testGetLimitStringWithMaxOnly() {
263263
assertEquals(
264264
"select distinct top(?) product2x0_.id as id0_, product2x0_.description as descript2_0_ " +
265265
"from Product2 product2x0_ order by product2x0_.id",
266-
withLimit( distinctQuery, toRowSelection( 0, 5 ) )
266+
withLimit( distinctQuery, toRowSelection( null, 5 ) )
267267
);
268268
}
269269

@@ -406,14 +406,14 @@ public void testGetLimitStringWithSelectClauseNestedQueryUsingParenthesisOnlyTop
406406

407407
assertEquals(
408408
"select top(?) t1.c1 as col_0_0, (select case when count(t2.c1)>0 then 'ADDED' else 'UNMODIFIED' end from table2 t2 WHERE (t2.c1 in (?))) as col_1_0 from table1 t1 WHERE 1=1 ORDER BY t1.c1 ASC",
409-
withLimit( query, toRowSelection( 0, 5 ) )
409+
withLimit( query, toRowSelection( null, 5 ) )
410410
);
411411
}
412412

413413
@Test
414414
@JiraKey(value = "HHH-8916")
415415
public void testGetLimitStringUsingCTEQueryNoOffset() {
416-
Limit selection = toRowSelection( 0, 5 );
416+
Limit selection = toRowSelection( null, 5 );
417417

418418
// test top-based CTE with single CTE query_ definition with no odd formatting
419419
final String query1 = "WITH a (c1, c2) AS (SELECT c1, c2 FROM t) SELECT c1, c2 FROM a";
@@ -639,7 +639,7 @@ private String withLimit(String sql, Limit limit) {
639639
return dialect.getLimitHandler().processSql( sql, -1, null, new LimitQueryOptions( limit ) );
640640
}
641641

642-
private Limit toRowSelection(int firstRow, int maxRows) {
642+
private Limit toRowSelection(Integer firstRow, Integer maxRows) {
643643
Limit selection = new Limit();
644644
selection.setFirstRow( firstRow );
645645
selection.setMaxRows( maxRows );

hibernate-community-dialects/src/test/java/org/hibernate/community/dialect/SQLServer2008DialectTestCase.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ public void testGetLimitStringWithSelectDistinctSubselect() {
169169
"select top(?) col0_.CONTENTID as CONTENT1_12_ " +
170170
"where col0_.CONTENTTYPE='PAGE' and (col0_.CONTENTID in " +
171171
"(select distinct col2_.PREVVER from CONTENT col2_ where (col2_.PREVVER is not null)))",
172-
withLimit( selectDistinctSubselectSQL, toRowSelection( 0, 5 ) )
172+
withLimit( selectDistinctSubselectSQL, toRowSelection( null, 5 ) )
173173
);
174174
}
175175

@@ -255,7 +255,7 @@ public void testGetLimitStringWithMaxOnly() {
255255
assertEquals(
256256
"select top(?) product2x0_.id as id0_, product2x0_.description as descript2_0_ " +
257257
"from Product2 product2x0_ order by product2x0_.id",
258-
withLimit( query, toRowSelection( 0, 1 ) )
258+
withLimit( query, toRowSelection( null, 1 ) )
259259
);
260260

261261
final String distinctQuery = "select distinct product2x0_.id as id0_, product2x0_.description as descript2_0_ " +
@@ -264,7 +264,7 @@ public void testGetLimitStringWithMaxOnly() {
264264
assertEquals(
265265
"select distinct top(?) product2x0_.id as id0_, product2x0_.description as descript2_0_ " +
266266
"from Product2 product2x0_ order by product2x0_.id",
267-
withLimit( distinctQuery, toRowSelection( 0, 5 ) )
267+
withLimit( distinctQuery, toRowSelection( null, 5 ) )
268268
);
269269
}
270270

@@ -407,14 +407,14 @@ public void testGetLimitStringWithSelectClauseNestedQueryUsingParenthesisOnlyTop
407407

408408
assertEquals(
409409
"select top(?) t1.c1 as col_0_0, (select case when count(t2.c1)>0 then 'ADDED' else 'UNMODIFIED' end from table2 t2 WHERE (t2.c1 in (?))) as col_1_0 from table1 t1 WHERE 1=1 ORDER BY t1.c1 ASC",
410-
withLimit( query, toRowSelection( 0, 5 ) )
410+
withLimit( query, toRowSelection( null, 5 ) )
411411
);
412412
}
413413

414414
@Test
415415
@JiraKey("HHH-8916")
416416
public void testGetLimitStringUsingCTEQueryNoOffset() {
417-
Limit selection = toRowSelection( 0, 5 );
417+
Limit selection = toRowSelection( null, 5 );
418418

419419
// test top-based CTE with single CTE query_ definition with no odd formatting
420420
final String query1 = "WITH a (c1, c2) AS (SELECT c1, c2 FROM t) SELECT c1, c2 FROM a";
@@ -640,7 +640,7 @@ private String withLimit(String sql, Limit limit) {
640640
return dialect.getLimitHandler().processSql( sql, -1, null, new LimitQueryOptions( limit ) );
641641
}
642642

643-
private Limit toRowSelection(int firstRow, int maxRows) {
643+
private Limit toRowSelection(Integer firstRow, Integer maxRows) {
644644
Limit selection = new Limit();
645645
selection.setFirstRow( firstRow );
646646
selection.setMaxRows( maxRows );

hibernate-community-dialects/src/test/java/org/hibernate/community/dialect/SQLServer2012DialectTestCase.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public void testGetLimitStringMaxRowsOnly() {
4444
final String input = "select distinct f1 as f53245 from table846752 order by f234, f67 desc";
4545
assertEquals(
4646
input + " offset 0 rows fetch first ? rows only",
47-
withLimit( input, toRowSelection( 0, 10 ) ).toLowerCase( Locale.ROOT )
47+
withLimit( input, toRowSelection( null, 10 ) ).toLowerCase( Locale.ROOT )
4848
);
4949
}
5050

@@ -64,7 +64,7 @@ public void testGetLimitStringMaxRowsOnlyNoOrderBy() {
6464
final String input = "select f1 from table";
6565
assertEquals(
6666
"select f1 from table order by @@version offset 0 rows fetch first ? rows only",
67-
withLimit( input, toRowSelection( 0, 10 ) ).toLowerCase( Locale.ROOT )
67+
withLimit( input, toRowSelection( null, 10 ) ).toLowerCase( Locale.ROOT )
6868
);
6969
}
7070

@@ -82,7 +82,7 @@ private String withLimit(String sql, Limit limit) {
8282
return dialect.getLimitHandler().processSql( sql, -1, null, new LimitQueryOptions( limit ) );
8383
}
8484

85-
private Limit toRowSelection(int firstRow, int maxRows) {
85+
private Limit toRowSelection(Integer firstRow, Integer maxRows) {
8686
final Limit selection = new Limit();
8787
selection.setFirstRow( firstRow );
8888
selection.setMaxRows( maxRows );

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,7 @@ public static boolean hasMaxRows(Limit limit) {
233233
*/
234234
public static boolean hasFirstRow(Limit limit) {
235235
return limit != null
236-
&& limit.getFirstRow() != null
237-
&& limit.getFirstRow() > 0;
236+
&& limit.getFirstRow() != null;
238237
}
239238

240239
/**

hibernate-core/src/test/java/org/hibernate/orm/test/dialect/AbstractLimitHandlerTest.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ protected void assertGenerateExpectedSql(String expected, String sql) {
4848
protected abstract LimitHandler getLimitHandler();
4949

5050
protected Limit getLimit() {
51-
return new Limit(0, 10);
51+
return new Limit(null, 10);
5252
}
5353

5454
protected String getLimitClause() {
@@ -62,9 +62,13 @@ protected String getLimitClause() {
6262
}
6363
else if (hasFirstRow(limit)) {
6464
return " offset " + (oflh.supportsVariableLimit() ? "?" : String.valueOf(limit.getFirstRow())) + " rows";
65-
} else {
65+
}
66+
else if (hasMaxRows(limit)) {
6667
return " fetch first " + (oflh.supportsVariableLimit() ? "?" : String.valueOf(limit.getMaxRows())) + " rows only";
6768
}
69+
else {
70+
return "";
71+
}
6872
}
6973
return " limit ?";
7074
}

0 commit comments

Comments
 (0)