HHH-1804 HHH-5096 : FetchingScrollableResultsImpl throws SQLException when empty; last() does not move cursor if it is after last

git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@19217 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
Gail Badner 2010-04-12 18:21:12 +00:00
parent 643ecc8060
commit 780666b65a
2 changed files with 215 additions and 1 deletions

View File

@ -75,6 +75,12 @@ public class FetchingScrollableResultsImpl extends AbstractScrollableResults {
return false;
}
if ( isResultSetEmpty() ) {
currentRow = null;
currentPosition = 0;
return false;
}
Object row = getLoader().loadSequentialRowsForward(
getResultSet(),
getSession(),
@ -183,13 +189,16 @@ public class FetchingScrollableResultsImpl extends AbstractScrollableResults {
public boolean last() throws HibernateException {
boolean more = false;
if ( maxPosition != null ) {
if ( currentPosition > maxPosition.intValue() ) {
more = previous();
}
for ( int i = currentPosition; i < maxPosition.intValue(); i++ ) {
more = next();
}
}
else {
try {
if ( getResultSet().isAfterLast() ) {
if ( isResultSetEmpty() || getResultSet().isAfterLast() ) {
// should not be able to reach last without maxPosition being set
// unless there are no results
return false;
@ -313,4 +322,18 @@ public class FetchingScrollableResultsImpl extends AbstractScrollableResults {
}
return scroll( rowNumber - currentPosition );
}
private boolean isResultSetEmpty() {
try {
return currentPosition == 0 && ! getResultSet().isBeforeFirst() && ! getResultSet().isAfterLast();
}
catch( SQLException e ) {
throw JDBCExceptionHelper.convert(
getSession().getFactory().getSQLExceptionConverter(),
e,
"Could not determine if resultset is empty due to exception calling isBeforeFirst or isAfterLast()"
);
}
}
}

View File

@ -1,6 +1,8 @@
// $Id: ScrollableCollectionFetchingTest.java 10977 2006-12-12 23:28:04Z steve.ebersole@jboss.com $
package org.hibernate.test.hql;
import java.util.List;
import junit.framework.Test;
import org.hibernate.HibernateException;
@ -47,6 +49,195 @@ public class ScrollableCollectionFetchingTest extends FunctionalTestCase {
s.close();
}
public void testScrollingJoinFetchesEmptyResultSet() {
Session s = openSession();
Transaction txn = s.beginTransaction();
assertTrue(s
.createQuery( "from Animal a left join fetch a.offspring where a.description like :desc order by a.id" )
.setString( "desc", "root%" )
.list()
.isEmpty() );
ScrollableResults results = s
.createQuery( "from Animal a left join fetch a.offspring where a.description like :desc order by a.id" )
.setString( "desc", "root%" )
.scroll();
assertFalse( results.isFirst() );
assertFalse( results.isLast() );
assertFalse( results.next() );
assertFalse( results.isFirst() );
assertFalse( results.isLast() );
assertFalse( results.previous() );
assertFalse( results.isFirst() );
assertFalse( results.isLast() );
results.beforeFirst();
assertFalse( results.isFirst() );
assertFalse( results.isLast() );
assertFalse( results.next() );
assertFalse( results.first() );
assertFalse( results.isFirst() );
assertFalse( results.isLast() );
assertFalse( results.next() );
results.afterLast();
assertFalse( results.isFirst() );
assertFalse( results.isLast() );
assertFalse( results.next() );
assertFalse( results.last() );
assertFalse( results.isFirst() );
assertFalse( results.isLast() );
assertFalse( results.next() );
for ( int i=1; i<3; i++ ) {
assertFalse( results.scroll( i ) );
assertFalse( results.isFirst() );
assertFalse( results.isLast() );
assertFalse( results.scroll( - i ) );
assertFalse( results.isFirst() );
assertFalse( results.isLast() );
assertFalse( results.setRowNumber( i ) );
assertFalse( results.isFirst() );
assertFalse( results.isLast() );
assertFalse( results.setRowNumber( - i ) );
assertFalse( results.isFirst() );
assertFalse( results.isLast() );
}
txn.commit();
s.close();
}
public void testScrollingJoinFetchesSingleRowResultSet() {
Session s = openSession();
Transaction txn = s.beginTransaction();
Animal mother = new Animal();
mother.setDescription( "root-1" );
Animal daughter = new Animal();
daughter.setDescription( "daughter" );
daughter.setMother( mother );
mother.addOffspring( daughter );
s.save( mother );
s.save( daughter );
txn.commit();
s.close();
s = openSession();
txn = s.beginTransaction();
assertNotNull(s
.createQuery( "from Animal a left join fetch a.offspring where a.description like :desc order by a.id" )
.setString( "desc", "root%" )
.uniqueResult() );
ScrollableResults results = s
.createQuery( "from Animal a left join fetch a.offspring where a.description like :desc order by a.id" )
.setString( "desc", "root%" )
.scroll();
assertFalse( results.isFirst() );
assertFalse( results.isLast() );
assertFalse( results.previous() );
assertTrue( results.next() );
assertTrue( results.isFirst() );
assertTrue( results.isLast() );
assertFalse( results.next() );
assertFalse( results.isFirst() );
assertFalse( results.isLast() );
assertTrue( results.previous() );
assertTrue( results.isFirst() );
assertTrue( results.isLast() );
assertFalse( results.previous() );
assertFalse( results.isFirst() );
assertFalse( results.isLast() );
assertTrue( results.next() );
assertTrue( results.isFirst() );
assertTrue( results.isLast() );
results.beforeFirst();
assertFalse( results.isFirst() );
assertFalse( results.isLast() );
assertFalse( results.previous() );
assertTrue( results.first() );
assertTrue( results.isFirst() );
assertTrue( results.isLast() );
assertFalse( results.next() );
results.afterLast();
assertFalse( results.isFirst() );
assertFalse( results.isLast() );
assertFalse( results.next() );
assertTrue( results.last() );
assertTrue( results.isFirst() );
assertTrue( results.isLast() );
assertFalse( results.next() );
assertTrue( results.first() );
assertTrue( results.isFirst() );
assertTrue( results.isLast() );
for ( int i=1; i<3; i++ ) {
assertTrue( results.setRowNumber( 1 ) );
assertTrue( results.isFirst() );
assertTrue( results.isLast() );
assertFalse( results.scroll( i ) );
assertFalse( results.isFirst() );
assertFalse( results.isLast() );
assertTrue( results.setRowNumber( 1 ) );
assertTrue( results.isFirst() );
assertTrue( results.isLast() );
assertFalse( results.scroll( - i ) );
assertFalse( results.isFirst() );
assertFalse( results.isLast() );
if ( i != 1 ) {
assertFalse( results.setRowNumber( i ) );
assertFalse( results.isFirst() );
assertFalse( results.isLast() );
assertFalse( results.setRowNumber( - i ) );
assertFalse( results.isFirst() );
assertFalse( results.isLast() );
}
}
txn.commit();
s.close();
s = openSession();
txn = s.beginTransaction();
s.createQuery( "delete Animal where not description like 'root%'" ).executeUpdate();
s.createQuery( "delete Animal" ).executeUpdate();
txn.commit();
s.close();
}
public void testScrollingJoinFetchesForward() {
if ( ! supportsResultSetPositionQueryMethodsOnForwardOnlyCursor() ) {
return;