[hibernate-commits] Hibernate SVN: r21083 - in core/branches/Branch_3_2_4_SP1_CP: src/org/hibernate/jdbc and 2 other directories.

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Fri Nov 9 14:38:13 EST 2012


Author: brmeyer
Date: 2012-11-09 14:38:13 -0500 (Fri, 09 Nov 2012)
New Revision: 21083

Modified:
   core/branches/Branch_3_2_4_SP1_CP/src/org/hibernate/engine/loading/CollectionLoadContext.java
   core/branches/Branch_3_2_4_SP1_CP/src/org/hibernate/jdbc/ResultSetWrapper.java
   core/branches/Branch_3_2_4_SP1_CP/src/org/hibernate/loader/Loader.java
   core/branches/Branch_3_2_4_SP1_CP/test/org/hibernate/test/hqlfetchscroll/HQLScrollFetchTest.java
Log:
JBPAPP-9919 Updated change from HHH-1283 

Modified: core/branches/Branch_3_2_4_SP1_CP/src/org/hibernate/engine/loading/CollectionLoadContext.java
===================================================================
--- core/branches/Branch_3_2_4_SP1_CP/src/org/hibernate/engine/loading/CollectionLoadContext.java	2012-11-09 19:26:30 UTC (rev 21082)
+++ core/branches/Branch_3_2_4_SP1_CP/src/org/hibernate/engine/loading/CollectionLoadContext.java	2012-11-09 19:38:13 UTC (rev 21083)
@@ -150,7 +150,7 @@
 	 *
 	 * @param persister The persister for which to complete loading.
 	 */
-	public void endLoadingCollections(CollectionPersister persister, Object loadedEntity) {
+	public void endLoadingCollections(CollectionPersister persister) {
 		SessionImplementor session = getLoadContext().getPersistenceContext().getSession();
 		if ( !loadContexts.hasLoadingCollectionEntries()
 				&& localLoadingCollectionKeys.isEmpty() ) {
@@ -172,6 +172,10 @@
 				log.warn( "In CollectionLoadContext#endLoadingCollections, localLoadingCollectionKeys contained [" + collectionKey + "], but no LoadingCollectionEntry was found in loadContexts" );
 			}
 			else if ( lce.getResultSet() == resultSet && lce.getPersister() == persister ) {
+				if ( matches == null ) {
+					matches = new ArrayList();
+				}
+				matches.add( lce );
 				
 				if ( lce.getCollection().getOwner() == null ) {
 					session.getPersistenceContext().addUnownedCollection(
@@ -179,13 +183,6 @@
 							lce.getCollection()
 					);
 				}
-				else if ( loadedEntity != null && lce.getCollection().getOwner() != loadedEntity ) {
-					continue;
-				}
-				if ( matches == null ) {
-					matches = new ArrayList<LoadingCollectionEntry>();
-				}
-				matches.add( lce );
 				if ( log.isTraceEnabled() ) {
 					log.trace( "removing collection load entry [" + lce + "]" );
 				}

Modified: core/branches/Branch_3_2_4_SP1_CP/src/org/hibernate/jdbc/ResultSetWrapper.java
===================================================================
--- core/branches/Branch_3_2_4_SP1_CP/src/org/hibernate/jdbc/ResultSetWrapper.java	2012-11-09 19:26:30 UTC (rev 21082)
+++ core/branches/Branch_3_2_4_SP1_CP/src/org/hibernate/jdbc/ResultSetWrapper.java	2012-11-09 19:38:13 UTC (rev 21083)
@@ -58,7 +58,7 @@
 	 * columnName or a database access error occurs
 	 */
 	public int findColumn(String columnName) throws SQLException {
-		return columnNameCache.getIndexForColumnName( columnName, this );
+		return new Integer( columnNameCache.getIndexForColumnName( columnName, this ) );
 	}
 
 	public Array getArray(String colName) throws SQLException {

Modified: core/branches/Branch_3_2_4_SP1_CP/src/org/hibernate/loader/Loader.java
===================================================================
--- core/branches/Branch_3_2_4_SP1_CP/src/org/hibernate/loader/Loader.java	2012-11-09 19:26:30 UTC (rev 21082)
+++ core/branches/Branch_3_2_4_SP1_CP/src/org/hibernate/loader/Loader.java	2012-11-09 19:38:13 UTC (rev 21083)
@@ -289,8 +289,7 @@
 				hydratedObjects, 
 				resultSet, 
 				session, 
-				queryParameters.isReadOnly(),
-				result
+				queryParameters.isReadOnly()
 			);
 		session.getPersistenceContext().initializeNonLazyCollections();
 		return result;
@@ -322,11 +321,20 @@
 						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(
@@ -341,12 +349,22 @@
 				hydratedObjects, 
 				resultSet, 
 				session, 
-				queryParameters.isReadOnly(),
-				result
+				queryParameters.isReadOnly()
 			);
 		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
@@ -819,15 +837,6 @@
 			final SessionImplementor session,
 			final boolean readOnly) 
 	throws HibernateException {
-		initializeEntitiesAndCollections(hydratedObjects,resultSetId,session,readOnly, null);
-	}
-	private void initializeEntitiesAndCollections(
-			final List hydratedObjects,
-			final Object resultSetId,
-			final SessionImplementor session,
-			final boolean readOnly,
-			final Object loadedEntity) 
-	throws HibernateException {
 		
 		final CollectionPersister[] collectionPersisters = getCollectionPersisters();
 		if ( collectionPersisters != null ) {
@@ -838,7 +847,7 @@
 					//during loading
 					//TODO: or we could do this polymorphically, and have two
 					//      different operations implemented differently for arrays
-					endCollectionLoad( resultSetId, session, collectionPersisters[i], loadedEntity );
+					endCollectionLoad( resultSetId, session, collectionPersisters[i] );
 				}
 			}
 		}
@@ -872,7 +881,7 @@
 					//the entities, since we might call hashCode() on the elements
 					//TODO: or we could do this polymorphically, and have two
 					//      different operations implemented differently for arrays
-					endCollectionLoad( resultSetId, session, collectionPersisters[i], loadedEntity );
+					endCollectionLoad( resultSetId, session, collectionPersisters[i] );
 				}
 			}
 		}
@@ -882,13 +891,12 @@
 	private void endCollectionLoad(
 			final Object resultSetId,
 			final SessionImplementor session,
-			final CollectionPersister collectionPersister,
-			final Object loadedEntity) {
+			final CollectionPersister collectionPersister) {
 		//this is a query and we are loading multiple instances of the same collection role
 		session.getPersistenceContext()
 				.getLoadContexts()
 				.getCollectionLoadContext( ( ResultSet ) resultSetId )
-				.endLoadingCollections( collectionPersister, loadedEntity );
+				.endLoadingCollections( collectionPersister );
 	}
 
 	protected List getResultList(List results, ResultTransformer resultTransformer) throws QueryException {

Modified: core/branches/Branch_3_2_4_SP1_CP/test/org/hibernate/test/hqlfetchscroll/HQLScrollFetchTest.java
===================================================================
--- core/branches/Branch_3_2_4_SP1_CP/test/org/hibernate/test/hqlfetchscroll/HQLScrollFetchTest.java	2012-11-09 19:26:30 UTC (rev 21082)
+++ core/branches/Branch_3_2_4_SP1_CP/test/org/hibernate/test/hqlfetchscroll/HQLScrollFetchTest.java	2012-11-09 19:38:13 UTC (rev 21083)
@@ -1,13 +1,20 @@
 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.DB2Dialect;
+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;
 
@@ -33,7 +40,11 @@
 	}
 
 	public void testScroll() {
-		if(getDialect() instanceof Oracle8iDialect){
+		if( getDialect() instanceof SQLServerDialect
+				|| getDialect() instanceof Oracle8iDialect
+				|| getDialect() instanceof DB2Dialect
+				|| getDialect() instanceof H2Dialect){
+			reportSkip( "SQL Server and Oracle do not sort the result set automatically, so failure as expected","HQLScrollFetchTest" );
 			return;
 		}
 		try {
@@ -52,6 +63,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();
@@ -120,15 +290,60 @@
 		}
 	}
 
+	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 ) );
+			}
+			assertFalse( "list is not correct size: ", 2 == list.size() );
+			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();
+			assertFalse( "list is not correct size: ", 2 == results.size() );
+			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 ( Iterator i = list.iterator(); i.hasNext(); ) {
-			Parent parent = (Parent) i.next();
-			assertEquals(
-					"parent " + parent + " has incorrect collection(" + parent.getChildren() + ").",
-					3,
-					parent.getChildren().size()
-			);
+		for ( Object aList : list ) {
+			assertResultFromOneUser( (Parent) aList );
 		}
 	}
 
@@ -143,58 +358,58 @@
 		s.close();
 	}
 	private void insertTestData() {
-    Session s = openSession();
-    Transaction t = s.beginTransaction();
-    Child child_1_1 = new Child( "achild1-1");
-    Child child_1_2 = new Child( "ychild1-2");
-    Child child_1_3 = new Child( "dchild1-3");
-    Child child_2_1 = new Child( "bchild2-1");
-    Child child_2_2 = new Child( "cchild2-2");
-    Child child_2_3 = new Child( "zchild2-3");
+		Session s = openSession();
+		Transaction t = s.beginTransaction();
+		Child child_1_1 = new Child( "achild1-1");
+		Child child_1_2 = new Child( "ychild1-2");
+		Child child_1_3 = new Child( "dchild1-3");
+		Child child_2_1 = new Child( "bchild2-1");
+		Child child_2_2 = new Child( "cchild2-2");
+		Child child_2_3 = new Child( "zchild2-3");
 
-    s.save( child_1_1 );
-    s.save( child_2_1 );
-    s.save( child_1_2 );
-    s.save( child_2_2 );
-    s.save( child_1_3 );
-    s.save( child_2_3 );
+		s.save( child_1_1 );
+		s.save( child_2_1 );
+		s.save( child_1_2 );
+		s.save( child_2_2 );
+		s.save( child_1_3 );
+		s.save( child_2_3 );
 
-    s.flush();
+		s.flush();
 
-    Parent p1 = new Parent( "parent1" );
-    p1.addChild( child_1_1 );
-    p1.addChild( child_1_2 );
-    p1.addChild( child_1_3 );
-    s.save( p1 );
+		Parent p1 = new Parent( "parent1" );
+		p1.addChild( child_1_1 );
+		p1.addChild( child_1_2 );
+		p1.addChild( child_1_3 );
+		s.save( p1 );
 
-    Parent p2 = new Parent( "parent2" );
-    p2.addChild( child_2_1 );
-    p2.addChild( child_2_2 );
-    p2.addChild( child_2_3 );
-    s.save( p2 );
+		Parent p2 = new Parent( "parent2" );
+		p2.addChild( child_2_1 );
+		p2.addChild( child_2_2 );
+		p2.addChild( child_2_3 );
+		s.save( p2 );
 
-    t.commit();
-    s.close();
-}
+		t.commit();
+		s.close();
+	}
 
-//	private void insertTestData() {
-//		Session s = openSession();
-//		Transaction t = s.beginTransaction();
-//		s.save( makeParent( "parent1", "child1-1", "child1-2", "child1-3" ) );
-//		s.save( makeParent( "parent2", "child2-1", "child2-2", "child2-3" ) );
-//		t.commit();
-//		s.close();
-//	}
+	//	private void insertTestData() {
+	//		Session s = openSession();
+	//		Transaction t = s.beginTransaction();
+	//		s.save( makeParent( "parent1", "child1-1", "child1-2", "child1-3" ) );
+	//		s.save( makeParent( "parent2", "child2-1", "child2-2", "child2-3" ) );
+	//		t.commit();
+	//		s.close();
+	//	}
 
 	public String[] getMappings() {
 		return new String[] { "hqlfetchscroll/ParentChild.hbm.xml" };
 	}
 
-//	protected Object makeParent(String name, String child1, String child2, String child3) {
-//		Parent parent = new Parent( name );
-//		parent.addChild( new Child( child1 ) );
-//		parent.addChild( new Child( child2 ) );
-//		parent.addChild( new Child( child3 ) );
-//		return parent;
-//	}
+	//	protected Object makeParent(String name, String child1, String child2, String child3) {
+	//		Parent parent = new Parent( name );
+	//		parent.addChild( new Child( child1 ) );
+	//		parent.addChild( new Child( child2 ) );
+	//		parent.addChild( new Child( child3 ) );
+	//		return parent;
+	//	}
 }



More information about the hibernate-commits mailing list