Skip to content

Commit 74ecc8b

Browse files
committed
HHH-19266 add getPosition() and deprecate get/setRowNumber()
since position() was a dupe of setRowNumber()
1 parent 9a7b95b commit 74ecc8b

File tree

7 files changed

+58
-7
lines changed

7 files changed

+58
-7
lines changed

hibernate-core/src/main/java/org/hibernate/ScrollableResults.java

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,19 +57,41 @@ public interface ScrollableResults<R> extends AutoCloseable {
5757
* position.
5858
*
5959
* @param positions a positive (forward) or negative (backward)
60-
* number of rows
60+
* number of positions
6161
*
6262
* @return {@code true} if there is a result at the new location
6363
*/
6464
boolean scroll(int positions);
6565

6666
/**
67-
* Moves the result cursor to the specified position.
67+
* Moves the result cursor to the specified position. The index
68+
* may be a positive value, and the position may be reached by
69+
* counting forward from the first result at position {@code 1},
70+
* or it may be a negative value, so that the position may be
71+
* reached by counting backward from the last result at position
72+
* {@code -1}.
73+
*
74+
* @param position an absolute positive (from the start) or
75+
* negative (from the end) position within the
76+
* query results
6877
*
6978
* @return {@code true} if there is a result at the new location
7079
*/
7180
boolean position(int position);
7281

82+
/**
83+
* The current position within the query results. The first
84+
* query result, if any, is at position {@code 1}. An empty
85+
* or newly-created instance has position {@code 0}.
86+
*
87+
* @return the current position, a positive integer index
88+
* starting at {@code 1}, or {@code 0} if this
89+
* instance is empty or newly-created
90+
*
91+
* @since 7.0
92+
*/
93+
int getPosition();
94+
7395
/**
7496
* Go to the last result.
7597
*
@@ -119,7 +141,10 @@ public interface ScrollableResults<R> extends AutoCloseable {
119141
*
120142
* @return The current position number, numbered from 1;
121143
* -1 indicates that there is no current row
144+
*
145+
* @deprecated Use {@link #getPosition()}
122146
*/
147+
@Deprecated(since = "7", forRemoval = true)
123148
int getRowNumber();
124149

125150
/**
@@ -136,7 +161,10 @@ public interface ScrollableResults<R> extends AutoCloseable {
136161
* from the last row.
137162
*
138163
* @return true if there is a row at that row number
164+
*
165+
* @deprecated Use {@link #position(int)}
139166
*/
167+
@Deprecated(since = "7", forRemoval = true)
140168
boolean setRowNumber(int rowNumber);
141169

142170
/**

hibernate-core/src/main/java/org/hibernate/internal/EmptyScrollableResults.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@ public int getRowNumber() {
8181
return 0;
8282
}
8383

84+
@Override
85+
public int getPosition() {
86+
return 0;
87+
}
88+
8489
@Override
8590
public boolean setRowNumber(int rowNumber) {
8691
return false;

hibernate-core/src/main/java/org/hibernate/internal/FetchingScrollableResultsImpl.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,11 @@ public int getRowNumber() {
270270
return currentPosition;
271271
}
272272

273+
@Override
274+
public int getPosition() {
275+
return currentPosition;
276+
}
277+
273278
@Override
274279
public boolean setRowNumber(int rowNumber) {
275280
if ( rowNumber == 1 ) {

hibernate-core/src/main/java/org/hibernate/internal/ScrollableResultsImpl.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,12 @@ public boolean isLast() {
107107

108108
@Override
109109
public int getRowNumber() {
110-
return getRowProcessingState().getPosition() + 1;
110+
return getPosition();
111+
}
112+
113+
@Override
114+
public int getPosition() {
115+
return getRowProcessingState().getPosition();
111116
}
112117

113118
@Override

hibernate-core/src/main/java/org/hibernate/sql/results/jdbc/internal/JdbcValuesCacheHit.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ protected boolean processScroll(int numberOfRows, RowProcessingState rowProcessi
7171

7272
position += numberOfRows;
7373

74-
if ( position > this.numberOfRows ) {
74+
if ( position >= this.numberOfRows ) {
7575
position = this.numberOfRows;
7676
return false;
7777
}
@@ -81,7 +81,7 @@ protected boolean processScroll(int numberOfRows, RowProcessingState rowProcessi
8181

8282
@Override
8383
public int getPosition() {
84-
return position;
84+
return position + 1;
8585
}
8686

8787
@Override
@@ -93,8 +93,12 @@ protected boolean processPosition(int position, RowProcessingState rowProcessing
9393
// we need to subtract it from `numberOfRows`
9494
position = numberOfRows + position;
9595
}
96+
else {
97+
// internally, positions are indexed from zero
98+
position--;
99+
}
96100

97-
if ( position > numberOfRows ) {
101+
if ( position >= numberOfRows ) {
98102
this.position = numberOfRows;
99103
return false;
100104
}

hibernate-core/src/main/java/org/hibernate/sql/results/jdbc/internal/JdbcValuesResultSetImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ private boolean scrollRows(final int numberOfRows) {
170170
@Override
171171
public int getPosition() {
172172
try {
173-
return resultSet.getRow() - 1;
173+
return resultSet.getRow();
174174
}
175175
catch (SQLException e) {
176176
throw makeExecutionException( "Error calling ResultSet#getRow", e );

hibernate-core/src/test/java/org/hibernate/orm/test/hql/ScrollableCollectionFetchingTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,8 @@ public void testScrollingJoinFetchesWithNext(SessionFactoryScope scope) {
354354
.setParameter( "desc", "root%" )
355355
.scroll()) {
356356

357+
assertEquals( 0, results.getRowNumber() );
358+
357359
assertTrue( results.next() );
358360
Animal animal = (Animal) results.get();
359361
assertEquals( data.root1Id, animal.getId(), "next() did not return expected row" );
@@ -384,6 +386,8 @@ public void testScrollingNoJoinFetchesWithNext(SessionFactoryScope scope) {
384386
.setParameter( "desc", "root%" )
385387
.scroll()) {
386388

389+
assertEquals( 0, results.getRowNumber() );
390+
387391
assertTrue( results.next() );
388392
Animal animal = (Animal) results.get();
389393
assertEquals( data.root1Id, animal.getId(), "next() did not return expected row" );

0 commit comments

Comments
 (0)