[hibernate-commits] Hibernate SVN: r21082 - in core/branches/Branch_3_3_2_GA_CP: core/src/main/java/org/hibernate/engine/loading and 2 other directories.
hibernate-commits at lists.jboss.org
hibernate-commits at lists.jboss.org
Fri Nov 9 14:26:30 EST 2012
Author: brmeyer
Date: 2012-11-09 14:26:30 -0500 (Fri, 09 Nov 2012)
New Revision: 21082
Modified:
core/branches/Branch_3_3_2_GA_CP/core/src/main/java/org/hibernate/engine/jdbc/ResultSetWrapperProxy.java
core/branches/Branch_3_3_2_GA_CP/core/src/main/java/org/hibernate/engine/loading/CollectionLoadContext.java
core/branches/Branch_3_3_2_GA_CP/core/src/main/java/org/hibernate/loader/Loader.java
core/branches/Branch_3_3_2_GA_CP/testsuite/src/test/java/org/hibernate/test/hqlfetchscroll/HQLScrollFetchTest.java
Log:
JBPAPP-9918 Updated change from HHH-1283
Modified: core/branches/Branch_3_3_2_GA_CP/core/src/main/java/org/hibernate/engine/jdbc/ResultSetWrapperProxy.java
===================================================================
--- core/branches/Branch_3_3_2_GA_CP/core/src/main/java/org/hibernate/engine/jdbc/ResultSetWrapperProxy.java 2012-11-09 18:34:44 UTC (rev 21081)
+++ core/branches/Branch_3_3_2_GA_CP/core/src/main/java/org/hibernate/engine/jdbc/ResultSetWrapperProxy.java 2012-11-09 19:26:30 UTC (rev 21082)
@@ -131,7 +131,7 @@
* @throws SQLException if the ResultSet object does not contain columnName or a database access error occurs
*/
private int findColumn(String columnName) throws SQLException {
- return columnNameCache.getIndexForColumnName( columnName, rs );
+ return new Integer( columnNameCache.getIndexForColumnName( columnName, rs ) );
}
private boolean isFirstArgColumnLabel(Method method, Object args[]) {
@@ -177,7 +177,7 @@
private Object[] buildColumnIndexMethodArgs(Object[] incomingArgs, int columnIndex) {
Object actualArgs[] = new Object[incomingArgs.length];
- actualArgs[0] = columnIndex;
+ actualArgs[0] = new Integer( columnIndex );
System.arraycopy( incomingArgs, 1, actualArgs, 1, incomingArgs.length - 1 );
return actualArgs;
}
Modified: core/branches/Branch_3_3_2_GA_CP/core/src/main/java/org/hibernate/engine/loading/CollectionLoadContext.java
===================================================================
--- core/branches/Branch_3_3_2_GA_CP/core/src/main/java/org/hibernate/engine/loading/CollectionLoadContext.java 2012-11-09 18:34:44 UTC (rev 21081)
+++ core/branches/Branch_3_3_2_GA_CP/core/src/main/java/org/hibernate/engine/loading/CollectionLoadContext.java 2012-11-09 19:26:30 UTC (rev 21082)
@@ -173,7 +173,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() ) {
@@ -195,20 +195,17 @@
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(
new CollectionKey( persister, lce.getKey(), session.getEntityMode() ),
lce.getCollection()
);
}
- else if ( loadedEntity != null && lce.getCollection().getOwner() != loadedEntity ) {
- continue;
- }
- if ( matches == null ) {
- matches = new ArrayList();
- }
- matches.add( lce );
if ( log.isTraceEnabled() ) {
log.trace( "removing collection load entry [" + lce + "]" );
}
Modified: core/branches/Branch_3_3_2_GA_CP/core/src/main/java/org/hibernate/loader/Loader.java
===================================================================
--- core/branches/Branch_3_3_2_GA_CP/core/src/main/java/org/hibernate/loader/Loader.java 2012-11-09 18:34:44 UTC (rev 21081)
+++ core/branches/Branch_3_3_2_GA_CP/core/src/main/java/org/hibernate/loader/Loader.java 2012-11-09 19:26:30 UTC (rev 21082)
@@ -351,8 +351,7 @@
hydratedObjects,
resultSet,
session,
- queryParameters.isReadOnly(),
- result
+ queryParameters.isReadOnly()
);
session.getPersistenceContext().initializeNonLazyCollections();
return result;
@@ -384,11 +383,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(
@@ -403,12 +411,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
@@ -984,16 +1002,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 ) {
@@ -1004,7 +1012,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] );
}
}
}
@@ -1038,7 +1046,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] );
}
}
}
@@ -1048,13 +1056,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 );
}
/**
Modified: core/branches/Branch_3_3_2_GA_CP/testsuite/src/test/java/org/hibernate/test/hqlfetchscroll/HQLScrollFetchTest.java
===================================================================
--- core/branches/Branch_3_3_2_GA_CP/testsuite/src/test/java/org/hibernate/test/hqlfetchscroll/HQLScrollFetchTest.java 2012-11-09 18:34:44 UTC (rev 21081)
+++ core/branches/Branch_3_3_2_GA_CP/testsuite/src/test/java/org/hibernate/test/hqlfetchscroll/HQLScrollFetchTest.java 2012-11-09 19:26:30 UTC (rev 21082)
@@ -1,15 +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;
@@ -25,7 +30,7 @@
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();
}
@@ -35,7 +40,10 @@
}
public void testScroll() {
- if( getDialect() instanceof SQLServerDialect || getDialect() instanceof Oracle8iDialect || getDialect() instanceof DB2Dialect){
+ 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;
}
@@ -57,6 +65,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();
@@ -125,15 +292,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 );
}
}
@@ -148,58 +360,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