[hibernate-commits] Hibernate SVN: r21075 - in core/patches/hibernate-3.3.2.GA_CP04_JBPAPP-9921: testsuite/src/test/java/org/hibernate/test/hqlfetchscroll and 1 other directory.

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Fri Sep 14 11:31:33 EDT 2012


Author: brmeyer
Date: 2012-09-14 11:31:32 -0400 (Fri, 14 Sep 2012)
New Revision: 21075

Modified:
   core/patches/hibernate-3.3.2.GA_CP04_JBPAPP-9921/core/src/main/java/org/hibernate/loader/Loader.java
   core/patches/hibernate-3.3.2.GA_CP04_JBPAPP-9921/testsuite/src/test/java/org/hibernate/test/hqlfetchscroll/HQLScrollFetchTest.java
Log:
JBPAPP-9921 Support patch for HHH-1283

Modified: core/patches/hibernate-3.3.2.GA_CP04_JBPAPP-9921/core/src/main/java/org/hibernate/loader/Loader.java
===================================================================
--- core/patches/hibernate-3.3.2.GA_CP04_JBPAPP-9921/core/src/main/java/org/hibernate/loader/Loader.java	2012-09-14 14:18:29 UTC (rev 21074)
+++ core/patches/hibernate-3.3.2.GA_CP04_JBPAPP-9921/core/src/main/java/org/hibernate/loader/Loader.java	2012-09-14 15:31:32 UTC (rev 21075)
@@ -383,12 +383,21 @@
 						hydratedObjects,
 						loadedKeys,
 						returnProxies
+				);
+				if ( ! keyToRead.equals( loadedKeys[0] ) ) {
+					throw new AssertionFailure(
+							String.format(
+							"Unexpected key read for row; expected [%s]; actual [%s]",
+							keyToRead,
+							loadedKeys[0] )
 					);
+				}
 				if ( result == null ) {
 					result = loaded;
 				}
 			} 
-			while ( keyToRead.equals( loadedKeys[0] ) && resultSet.next() );
+			while ( resultSet.next() &&
+					isCurrentRowForSameEntity( keyToRead, 0, resultSet, session ) );
 		}
 		catch ( SQLException sqle ) {
 			throw JDBCExceptionHelper.convert(
@@ -409,6 +418,17 @@
 		session.getPersistenceContext().initializeNonLazyCollections();
 		return result;
 	}
+	
+	private boolean isCurrentRowForSameEntity(
+			final EntityKey keyToRead,
+			final int persisterIndex,
+			final ResultSet resultSet,
+			final SessionImplementor session) throws SQLException {
+		EntityKey currentRowKey = getKeyFromResultSet(
+				persisterIndex, getEntityPersisters()[persisterIndex], null, resultSet, session
+				);
+		return keyToRead.equals( currentRowKey );
+	}
 
 	/**
 	 * Loads a single logical row from the result set moving forward.  This is the

Modified: core/patches/hibernate-3.3.2.GA_CP04_JBPAPP-9921/testsuite/src/test/java/org/hibernate/test/hqlfetchscroll/HQLScrollFetchTest.java
===================================================================
--- core/patches/hibernate-3.3.2.GA_CP04_JBPAPP-9921/testsuite/src/test/java/org/hibernate/test/hqlfetchscroll/HQLScrollFetchTest.java	2012-09-14 14:18:29 UTC (rev 21074)
+++ core/patches/hibernate-3.3.2.GA_CP04_JBPAPP-9921/testsuite/src/test/java/org/hibernate/test/hqlfetchscroll/HQLScrollFetchTest.java	2012-09-14 15:31:32 UTC (rev 21075)
@@ -1,14 +1,19 @@
 package org.hibernate.test.hqlfetchscroll;
 
 import java.util.ArrayList;
+import java.util.HashSet;
 import java.util.Iterator;
 import java.util.List;
+import java.util.Set;
 
+import org.hibernate.Hibernate;
 import org.hibernate.ScrollableResults;
 import org.hibernate.Session;
 import org.hibernate.Transaction;
+import org.hibernate.dialect.H2Dialect;
 import org.hibernate.dialect.Oracle8iDialect;
 import org.hibernate.dialect.SQLServerDialect;
+import org.hibernate.engine.SessionImplementor;
 import org.hibernate.junit.functional.FunctionalTestCase;
 import org.hibernate.transform.DistinctRootEntityResultTransformer;
 
@@ -19,12 +24,12 @@
 	public HQLScrollFetchTest(String name) {
 		super( name );
 	}
-
+	
 	public void testNoScroll() {
 		try {
 			insertTestData();
 			Session s = openSession();
-			List list = s.createQuery( QUERY ).setResultTransformer(new DistinctRootEntityResultTransformer() ).list();
+			List list = s.createQuery( QUERY ).setResultTransformer( DistinctRootEntityResultTransformer.INSTANCE ).list();
 			assertResultFromAllUsers( list );
 			s.close();
 		}
@@ -34,7 +39,9 @@
 	}
 
 	public void testScroll() {
-		if( getDialect() instanceof SQLServerDialect || getDialect() instanceof Oracle8iDialect ){
+		if( getDialect() instanceof SQLServerDialect
+				|| getDialect() instanceof Oracle8iDialect
+				|| getDialect() instanceof H2Dialect ){
 			reportSkip( "SQL Server and Oracle do not sort the result set automatically, so failure as expected","HQLScrollFetchTest" );
 			return;
 		}
@@ -54,6 +61,165 @@
 		}
 	}
 
+	public void testIncompleteScrollFirstResult() {
+		try {
+			insertTestData();
+			Session s = openSession();
+			ScrollableResults results = s.createQuery( QUERY + " order by p.name asc" ).scroll();
+			results.next();
+			Parent p = (Parent) results.get( 0 );
+			assertResultFromOneUser( p );
+			s.close();
+		}
+		finally {
+			deleteAll();
+		}
+	}
+
+	public void testIncompleteScrollSecondResult() {
+		try {
+			insertTestData();
+			Session s = openSession();
+			ScrollableResults results = s.createQuery( QUERY + " order by p.name asc" ).scroll();
+			results.next();
+			Parent p = (Parent) results.get( 0 );
+			assertResultFromOneUser( p );
+			results.next();
+			p = (Parent) results.get( 0 );
+			assertResultFromOneUser( p );
+			s.close();
+		}
+		finally {
+			deleteAll();
+		}
+	}
+
+	public void testIncompleteScrollFirstResultInTransaction() {
+		try {
+			insertTestData();
+			Session s = openSession();
+			Transaction tx = s.beginTransaction();
+			ScrollableResults results = s.createQuery( QUERY + " order by p.name asc" ).scroll();
+			results.next();
+			Parent p = (Parent) results.get( 0 );
+			assertResultFromOneUser( p );
+			tx.commit();
+			s.close();
+		}
+		finally {
+			deleteAll();
+		}
+	}
+
+	public void testIncompleteScrollSecondResultInTransaction() {
+		try {
+			insertTestData();
+			Session s = openSession();
+			Transaction tx = s.beginTransaction();
+			ScrollableResults results = s.createQuery( QUERY + " order by p.name asc" ).scroll();
+			results.next();
+			Parent p = (Parent) results.get( 0 );
+			assertResultFromOneUser( p );
+			results.next();
+			p = (Parent) results.get( 0 );
+			assertResultFromOneUser( p );
+			tx.commit();
+			s.close();
+		}
+		finally {
+			deleteAll();
+		}
+	}
+
+	public void testIncompleteScroll() {
+		try {
+			insertTestData();
+			Session s = openSession();
+			ScrollableResults results = s.createQuery( QUERY + " order by p.name asc" ).scroll();
+			results.next();
+			Parent p = (Parent) results.get( 0 );
+			assertResultFromOneUser( p );
+			// get the other parent entity from the persistence context along with its first child
+			// retrieved from the resultset.
+			Parent pOther = null;
+			Child cOther = null;
+			for ( Object entity : ( (SessionImplementor) s ).getPersistenceContext().getEntitiesByKey().values() ) {
+				if ( Parent.class.isInstance( entity ) ) {
+					if ( entity != p ) {
+						if ( pOther != null ) {
+							fail( "unexpected parent found." );
+						}
+						pOther = (Parent) entity;
+					}
+				}
+				else if ( Child.class.isInstance( entity ) ) {
+					if ( ! p.getChildren().contains( entity ) ) {
+						if ( cOther != null ) {
+							fail( "unexpected child entity found" );
+						}
+						cOther = (Child) entity;
+					}
+				}
+				else {
+					fail( "unexpected type of entity." );
+				}
+			}
+			// check that the same second parent is obtained by calling Session.get()
+			assertNull( pOther );
+			assertNull( cOther );
+			s.close();
+		}
+		finally {
+			deleteAll();
+		}
+	}
+
+	public void testIncompleteScrollLast() {
+		try {
+			insertTestData();
+			Session s = openSession();
+			ScrollableResults results = s.createQuery( QUERY + " order by p.name asc" ).scroll();
+			results.next();
+			Parent p = (Parent) results.get( 0 );
+			assertResultFromOneUser( p );
+			results.last();
+			// get the other parent entity from the persistence context.
+			// since the result set was scrolled to the end, the other parent entity's collection has been
+			// properly initialized.
+			Parent pOther = null;
+			Set childrenOther = new HashSet();
+			for ( Object entity : ( ( SessionImplementor) s ).getPersistenceContext().getEntitiesByKey().values() ) {
+				if ( Parent.class.isInstance( entity ) ) {
+					if ( entity != p ) {
+						if ( pOther != null ) {
+							fail( "unexpected parent found." );
+						}
+						pOther = (Parent) entity;
+					}
+				}
+				else if ( Child.class.isInstance( entity ) ) {
+					if ( ! p.getChildren().contains( entity ) ) {
+						childrenOther.add( entity );
+					}
+				}
+				else {
+					fail( "unexpected type of entity." );
+				}
+			}
+			// check that the same second parent is obtained by calling Session.get()
+			assertSame( pOther, s.get( Parent.class, "parent2" ) );
+			assertNotNull( pOther );
+			// access pOther's collection; should be completely loaded
+			assertTrue( Hibernate.isInitialized( pOther.getChildren() ) );
+			assertEquals( childrenOther, pOther.getChildren() );
+			assertResultFromOneUser( pOther );
+			s.close();
+		}
+		finally {
+			deleteAll();
+		}
+	}
+
 	public void testScrollOrderParentAsc() {
 		try {
 			insertTestData();
@@ -122,15 +288,76 @@
 		}
 	}
 
-	private void assertResultFromAllUsers(List list) {
-		assertEquals( "list is not correct size: ", 2, list.size() );
-		for ( Iterator i = list.iterator(); i.hasNext(); ) {
-			Parent parent = (Parent) i.next();
-			assertEquals(
+	public void testScrollOrderChildrenDesc() {
+		try {
+			insertTestData();
+			Session s = openSession();
+			Transaction t = s.beginTransaction();
+			Parent p0 = new Parent( "parent0" );
+			s.save( p0 );
+			t.commit();
+			s.close();
+			s = openSession();
+			ScrollableResults results = s.createQuery( QUERY + " order by c.name desc" ).scroll();
+			List list = new ArrayList();
+			while ( results.next() ) {
+				list.add( results.get( 0 ) );
+			}
+			try {
+				assertResultFromAllUsers( list );
+				fail( "should have failed because data is ordered incorrectly." );
+			}
+			catch ( AssertionError ex ) {
+				// expected
+			}
+			finally {
+				s.close();
+			}
+		}
+		finally {
+			deleteAll();
+		}
+	}
+
+	public void testListOrderChildrenDesc() {
+		try {
+			insertTestData();
+			Session s = openSession();
+			Transaction t = s.beginTransaction();
+			Parent p0 = new Parent( "parent0" );
+			s.save( p0 );
+			t.commit();
+			s.close();
+			s = openSession();
+			List results = s.createQuery( QUERY + " order by c.name desc" ).list();
+			try {
+				assertResultFromAllUsers( results );
+				fail( "should have failed because data is ordered incorrectly." );
+			}
+			catch ( AssertionError ex ) {
+				// expected
+			}
+			finally {
+				s.close();
+			}
+		}
+		finally {
+			deleteAll();
+		}
+	}
+
+	private void assertResultFromOneUser(Parent parent) {
+		assertEquals(
 					"parent " + parent + " has incorrect collection(" + parent.getChildren() + ").",
 					3,
 					parent.getChildren().size()
-			);
+		);
+	}
+
+	private void assertResultFromAllUsers(List list) {
+		assertEquals( "list is not correct size: ", 2, list.size() );
+		for ( Object aList : list ) {
+			assertResultFromOneUser( (Parent) aList );
 		}
 	}
 



More information about the hibernate-commits mailing list