Hibernate SVN: r20925 - in core/branches/Branch_3_3_2_GA_CP: core/src/main/java/org/hibernate/engine/loading and 3 other directories.
by hibernate-commits@lists.jboss.org
Author: stliu
Date: 2011-04-06 01:26:11 -0400 (Wed, 06 Apr 2011)
New Revision: 20925
Added:
core/branches/Branch_3_3_2_GA_CP/testsuite/src/test/java/org/hibernate/test/hqlfetchscroll/
core/branches/Branch_3_3_2_GA_CP/testsuite/src/test/java/org/hibernate/test/hqlfetchscroll/Child.java
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/Parent.java
core/branches/Branch_3_3_2_GA_CP/testsuite/src/test/java/org/hibernate/test/hqlfetchscroll/ParentChild.hbm.xml
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
Log:
JBPAPP-5898 HHH-1283 ScrollableResults JoinFetch don't set child collection correctly
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 2011-04-06 03:56:19 UTC (rev 20924)
+++ core/branches/Branch_3_3_2_GA_CP/core/src/main/java/org/hibernate/engine/jdbc/ResultSetWrapperProxy.java 2011-04-06 05:26:11 UTC (rev 20925)
@@ -87,7 +87,7 @@
*/
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if ( "findColumn".equals( method.getName() ) ) {
- return new Integer( findColumn( ( String ) args[0] ) );
+ return findColumn( ( String ) args[0] );
}
if ( isFirstArgColumnLabel( method, args ) ) {
@@ -98,7 +98,7 @@
);
}
catch ( SQLException ex ) {
- StringBuffer buf = new StringBuffer()
+ StringBuilder buf = new StringBuilder()
.append( "Exception getting column index for column: [" )
.append( args[0] )
.append( "].\nReverting to using: [" )
@@ -109,7 +109,7 @@
JDBCExceptionReporter.logExceptions( ex, buf.toString() );
}
catch ( NoSuchMethodException ex ) {
- StringBuffer buf = new StringBuffer()
+ StringBuilder buf = new StringBuilder()
.append( "Exception switching from method: [" )
.append( method )
.append( "] to a method using the column index. Reverting to using: [" )
@@ -177,7 +177,7 @@
private Object[] buildColumnIndexMethodArgs(Object[] incomingArgs, int columnIndex) {
Object actualArgs[] = new Object[incomingArgs.length];
- actualArgs[0] = new Integer( columnIndex );
+ actualArgs[0] = 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 2011-04-06 03:56:19 UTC (rev 20924)
+++ core/branches/Branch_3_3_2_GA_CP/core/src/main/java/org/hibernate/engine/loading/CollectionLoadContext.java 2011-04-06 05:26:11 UTC (rev 20925)
@@ -173,7 +173,7 @@
*
* @param persister The persister for which to complete loading.
*/
- public void endLoadingCollections(CollectionPersister persister) {
+ public void endLoadingCollections(CollectionPersister persister, Object loadedEntity) {
SessionImplementor session = getLoadContext().getPersistenceContext().getSession();
if ( !loadContexts.hasLoadingCollectionEntries()
&& localLoadingCollectionKeys.isEmpty() ) {
@@ -195,16 +195,20 @@
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 2011-04-06 03:56:19 UTC (rev 20924)
+++ core/branches/Branch_3_3_2_GA_CP/core/src/main/java/org/hibernate/loader/Loader.java 2011-04-06 05:26:11 UTC (rev 20925)
@@ -351,7 +351,8 @@
hydratedObjects,
resultSet,
session,
- queryParameters.isReadOnly()
+ queryParameters.isReadOnly(),
+ result
);
session.getPersistenceContext().initializeNonLazyCollections();
return result;
@@ -402,7 +403,8 @@
hydratedObjects,
resultSet,
session,
- queryParameters.isReadOnly()
+ queryParameters.isReadOnly(),
+ result
);
session.getPersistenceContext().initializeNonLazyCollections();
return result;
@@ -976,12 +978,21 @@
return null;
}
}
+ private void initializeEntitiesAndCollections(
+ final List hydratedObjects,
+ final Object resultSetId,
+ 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 boolean readOnly,
+ final Object loadedEntity)
throws HibernateException {
final CollectionPersister[] collectionPersisters = getCollectionPersisters();
@@ -993,7 +1004,7 @@
//during loading
//TODO: or we could do this polymorphically, and have two
// different operations implemented differently for arrays
- endCollectionLoad( resultSetId, session, collectionPersisters[i] );
+ endCollectionLoad( resultSetId, session, collectionPersisters[i], loadedEntity );
}
}
}
@@ -1027,7 +1038,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] );
+ endCollectionLoad( resultSetId, session, collectionPersisters[i], loadedEntity );
}
}
}
@@ -1037,12 +1048,13 @@
private void endCollectionLoad(
final Object resultSetId,
final SessionImplementor session,
- final CollectionPersister collectionPersister) {
+ final CollectionPersister collectionPersister,
+ final Object loadedEntity) {
//this is a query and we are loading multiple instances of the same collection role
session.getPersistenceContext()
.getLoadContexts()
.getCollectionLoadContext( ( ResultSet ) resultSetId )
- .endLoadingCollections( collectionPersister );
+ .endLoadingCollections( collectionPersister, loadedEntity );
}
/**
Copied: core/branches/Branch_3_3_2_GA_CP/testsuite/src/test/java/org/hibernate/test/hqlfetchscroll/Child.java (from rev 20924, core/branches/Branch_3_2_4_SP1_CP/test/org/hibernate/test/hqlfetchscroll/Child.java)
===================================================================
--- core/branches/Branch_3_3_2_GA_CP/testsuite/src/test/java/org/hibernate/test/hqlfetchscroll/Child.java (rev 0)
+++ core/branches/Branch_3_3_2_GA_CP/testsuite/src/test/java/org/hibernate/test/hqlfetchscroll/Child.java 2011-04-06 05:26:11 UTC (rev 20925)
@@ -0,0 +1,25 @@
+package org.hibernate.test.hqlfetchscroll;
+
+public class Child {
+
+ private String name;
+
+ Child() {
+ }
+
+ public Child(String name) {
+ this.name = name;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ private void setName(String name) {
+ this.name = name;
+ }
+
+ public String toString() {
+ return name;
+ }
+}
\ No newline at end of file
Copied: core/branches/Branch_3_3_2_GA_CP/testsuite/src/test/java/org/hibernate/test/hqlfetchscroll/HQLScrollFetchTest.java (from rev 20924, core/branches/Branch_3_2_4_SP1_CP/test/org/hibernate/test/hqlfetchscroll/HQLScrollFetchTest.java)
===================================================================
--- core/branches/Branch_3_3_2_GA_CP/testsuite/src/test/java/org/hibernate/test/hqlfetchscroll/HQLScrollFetchTest.java (rev 0)
+++ core/branches/Branch_3_3_2_GA_CP/testsuite/src/test/java/org/hibernate/test/hqlfetchscroll/HQLScrollFetchTest.java 2011-04-06 05:26:11 UTC (rev 20925)
@@ -0,0 +1,196 @@
+package org.hibernate.test.hqlfetchscroll;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+import org.hibernate.ScrollableResults;
+import org.hibernate.Session;
+import org.hibernate.Transaction;
+import org.hibernate.junit.functional.FunctionalTestCase;
+import org.hibernate.transform.DistinctRootEntityResultTransformer;
+
+
+public class HQLScrollFetchTest extends FunctionalTestCase {
+ private static final String QUERY = "select p from Parent p join fetch p.children c";
+
+ public HQLScrollFetchTest(String name) {
+ super( name );
+ }
+
+ public void testNoScroll() {
+ try {
+ insertTestData();
+ Session s = openSession();
+ List list = s.createQuery( QUERY ).setResultTransformer(new DistinctRootEntityResultTransformer() ).list();
+ assertResultFromAllUsers( list );
+ s.close();
+ }
+ finally {
+ deleteAll();
+ }
+ }
+
+ public void testScroll() {
+ try {
+ insertTestData();
+ Session s = openSession();
+ ScrollableResults results = s.createQuery( QUERY ).scroll();
+ List list = new ArrayList();
+ while ( results.next() ) {
+ list.add( results.get( 0 ) );
+ }
+ assertResultFromAllUsers( list );
+ s.close();
+ }
+ finally {
+ deleteAll();
+ }
+ }
+
+ public void testScrollOrderParentAsc() {
+ try {
+ insertTestData();
+ Session s = openSession();
+ ScrollableResults results = s.createQuery( QUERY + " order by p.name asc" ).scroll();
+ List list = new ArrayList();
+ while ( results.next() ) {
+ list.add( results.get( 0 ) );
+ }
+ assertResultFromAllUsers( list );
+ s.close();
+ }
+ finally {
+ deleteAll();
+ }
+ }
+
+ public void testScrollOrderParentDesc() {
+ try {
+ insertTestData();
+ Session s = openSession();
+ ScrollableResults results = s.createQuery( QUERY + " order by p.name desc" ).scroll();
+ List list = new ArrayList();
+ while ( results.next() ) {
+ list.add( results.get( 0 ) );
+ }
+ assertResultFromAllUsers( list );
+ s.close();
+ }
+ finally {
+ deleteAll();
+ }
+ }
+
+ public void testScrollOrderParentAscChildrenAsc() {
+ try {
+ insertTestData();
+ Session s = openSession();
+ ScrollableResults results = s.createQuery( QUERY + " order by p.name asc, c.name asc" ).scroll();
+ List list = new ArrayList();
+ while ( results.next() ) {
+ list.add( results.get( 0 ) );
+ }
+ assertResultFromAllUsers( list );
+ s.close();
+ }
+ finally {
+ deleteAll();
+ }
+ }
+
+ public void testScrollOrderParentAscChildrenDesc() {
+ try {
+ insertTestData();
+ Session s = openSession();
+ ScrollableResults results = s.createQuery( QUERY + " order by p.name asc, c.name desc" ).scroll();
+ List list = new ArrayList();
+ while ( results.next() ) {
+ list.add( results.get( 0 ) );
+ }
+ assertResultFromAllUsers( list );
+ s.close();
+ }
+ finally {
+ deleteAll();
+ }
+ }
+
+ 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()
+ );
+ }
+ }
+
+ private void deleteAll() {
+ Session s = openSession();
+ Transaction t = s.beginTransaction();
+ List list = s.createQuery( "from Parent" ).list();
+ for ( Iterator i = list.iterator(); i.hasNext(); ) {
+ s.delete( (Parent) i.next() );
+ }
+ t.commit();
+ 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");
+
+ 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();
+
+ 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 );
+
+ 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;
+// }
+}
Copied: core/branches/Branch_3_3_2_GA_CP/testsuite/src/test/java/org/hibernate/test/hqlfetchscroll/Parent.java (from rev 20924, core/branches/Branch_3_2_4_SP1_CP/test/org/hibernate/test/hqlfetchscroll/Parent.java)
===================================================================
--- core/branches/Branch_3_3_2_GA_CP/testsuite/src/test/java/org/hibernate/test/hqlfetchscroll/Parent.java (rev 0)
+++ core/branches/Branch_3_3_2_GA_CP/testsuite/src/test/java/org/hibernate/test/hqlfetchscroll/Parent.java 2011-04-06 05:26:11 UTC (rev 20925)
@@ -0,0 +1,41 @@
+package org.hibernate.test.hqlfetchscroll;
+
+import java.util.HashSet;
+import java.util.Set;
+
+public class Parent {
+ private String name;
+ private Set children = new HashSet();
+
+ Parent() {
+ }
+
+ public Parent(String name) {
+ this.name = name;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+
+ void setName(String name) {
+ this.name = name;
+ }
+
+ public Set getChildren() {
+ return children;
+ }
+
+ private void setChildren(Set children) {
+ this.children = children;
+ }
+
+ public void addChild(Child child) {
+ children.add( child );
+ }
+
+ public String toString() {
+ return name;
+ }
+}
\ No newline at end of file
Copied: core/branches/Branch_3_3_2_GA_CP/testsuite/src/test/java/org/hibernate/test/hqlfetchscroll/ParentChild.hbm.xml (from rev 20924, core/branches/Branch_3_2_4_SP1_CP/test/org/hibernate/test/hqlfetchscroll/ParentChild.hbm.xml)
===================================================================
--- core/branches/Branch_3_3_2_GA_CP/testsuite/src/test/java/org/hibernate/test/hqlfetchscroll/ParentChild.hbm.xml (rev 0)
+++ core/branches/Branch_3_3_2_GA_CP/testsuite/src/test/java/org/hibernate/test/hqlfetchscroll/ParentChild.hbm.xml 2011-04-06 05:26:11 UTC (rev 20925)
@@ -0,0 +1,23 @@
+<?xml version="1.0"?>
+<!DOCTYPE hibernate-mapping PUBLIC
+ "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
+ "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
+
+<hibernate-mapping package="org.hibernate.test.hqlfetchscroll">
+
+ <class name="Parent" table="Parents">
+ <id name="name"/>
+
+ <set name="children" cascade="all-delete-orphan" lazy="false">
+ <key column="parent_id"/>
+ <one-to-many class="Child"/>
+ </set>
+
+ </class>
+
+ <class name="Child" table="chlidren">
+ <id name="name"/>
+ </class>
+
+
+</hibernate-mapping>