JPA Criteria do not load eagerly JOIN Fecth collections as Hibernate Criteria did
This commit is contained in:
parent
f6f1cb06d4
commit
dc21d565d3
|
@ -510,6 +510,14 @@ public class PluralAttributeMappingImpl
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
if ( collectionDescriptor.isLazy() ) {
|
||||||
|
return createDelayedCollectionFetch(
|
||||||
|
fetchParent,
|
||||||
|
fetchablePath,
|
||||||
|
creationState,
|
||||||
|
sqlAstCreationState
|
||||||
|
);
|
||||||
|
}
|
||||||
return new SelectEagerCollectionFetch( fetchablePath, this, fetchParent );
|
return new SelectEagerCollectionFetch( fetchablePath, this, fetchParent );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -518,11 +526,17 @@ public class PluralAttributeMappingImpl
|
||||||
return new SelectEagerCollectionFetch( fetchablePath, this, fetchParent );
|
return new SelectEagerCollectionFetch( fetchablePath, this, fetchParent );
|
||||||
}
|
}
|
||||||
|
|
||||||
final FromClauseAccess fromClauseAccess = sqlAstCreationState.getFromClauseAccess();
|
return createDelayedCollectionFetch( fetchParent, fetchablePath, creationState, sqlAstCreationState );
|
||||||
final TableGroup fetchParentTableGroup = fromClauseAccess.getTableGroup( fetchParent.getNavigablePath() );
|
}
|
||||||
|
|
||||||
|
private DelayedCollectionFetch createDelayedCollectionFetch(
|
||||||
|
FetchParent fetchParent,
|
||||||
|
NavigablePath fetchablePath,
|
||||||
|
DomainResultCreationState creationState,
|
||||||
|
SqlAstCreationState sqlAstCreationState) {
|
||||||
final DomainResult fkResult = getKeyDescriptor().createDomainResult(
|
final DomainResult fkResult = getKeyDescriptor().createDomainResult(
|
||||||
fetchablePath,
|
fetchablePath,
|
||||||
fetchParentTableGroup,
|
sqlAstCreationState.getFromClauseAccess().getTableGroup( fetchParent.getNavigablePath() ),
|
||||||
false,
|
false,
|
||||||
creationState
|
creationState
|
||||||
);
|
);
|
||||||
|
|
|
@ -29,6 +29,7 @@ import org.hibernate.sql.results.graph.Initializer;
|
||||||
import org.hibernate.sql.results.jdbc.spi.JdbcValues;
|
import org.hibernate.sql.results.jdbc.spi.JdbcValues;
|
||||||
import org.hibernate.sql.results.spi.RowReader;
|
import org.hibernate.sql.results.spi.RowReader;
|
||||||
import org.hibernate.sql.results.spi.RowTransformer;
|
import org.hibernate.sql.results.spi.RowTransformer;
|
||||||
|
import org.hibernate.stat.spi.StatisticsImplementor;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Steve Ebersole
|
* @author Steve Ebersole
|
||||||
|
@ -114,6 +115,11 @@ public class ResultsHelper {
|
||||||
final BatchFetchQueue batchFetchQueue = persistenceContext.getBatchFetchQueue();
|
final BatchFetchQueue batchFetchQueue = persistenceContext.getBatchFetchQueue();
|
||||||
batchFetchQueue.removeBatchLoadableCollection( collectionEntry );
|
batchFetchQueue.removeBatchLoadableCollection( collectionEntry );
|
||||||
|
|
||||||
|
final StatisticsImplementor statistics = persistenceContext.getSession().getFactory().getStatistics();
|
||||||
|
if ( statistics.isStatisticsEnabled() ) {
|
||||||
|
statistics.loadCollection( collectionDescriptor.getRole() );
|
||||||
|
}
|
||||||
|
|
||||||
// todo (6.0) : there is other logic still needing to be implemented here. caching, etc
|
// todo (6.0) : there is other logic still needing to be implemented here. caching, etc
|
||||||
// see org.hibernate.engine.loading.internal.CollectionLoadContext#endLoadingCollection in 5.x
|
// see org.hibernate.engine.loading.internal.CollectionLoadContext#endLoadingCollection in 5.x
|
||||||
}
|
}
|
||||||
|
|
|
@ -144,7 +144,7 @@ public class CollectionTest {
|
||||||
scope.inTransaction(
|
scope.inTransaction(
|
||||||
s -> {
|
s -> {
|
||||||
User u2 = findUser( s );
|
User u2 = findUser( s );
|
||||||
assertTrue( Hibernate.isInitialized( u2.getEmailAddresses() ) );
|
assertFalse( Hibernate.isInitialized( u2.getEmailAddresses() ) );
|
||||||
assertFalse( Hibernate.isInitialized( u2.getPermissions() ) );
|
assertFalse( Hibernate.isInitialized( u2.getPermissions() ) );
|
||||||
assertEquals( 2, u2.getEmailAddresses().size() );
|
assertEquals( 2, u2.getEmailAddresses().size() );
|
||||||
s.delete( u2 );
|
s.delete( u2 );
|
||||||
|
|
|
@ -944,6 +944,7 @@ public class DynamicFilterTest extends BaseNonConfigCoreFunctionalTestCase {
|
||||||
public void testManyToManyBaseThruCriteria() {
|
public void testManyToManyBaseThruCriteria() {
|
||||||
inSession(
|
inSession(
|
||||||
session -> {
|
session -> {
|
||||||
|
sessionFactory().getStatistics().clear();
|
||||||
CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();
|
CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();
|
||||||
CriteriaQuery<Product> criteria = criteriaBuilder.createQuery( Product.class );
|
CriteriaQuery<Product> criteria = criteriaBuilder.createQuery( Product.class );
|
||||||
Root<Product> root = criteria.from( Product.class );
|
Root<Product> root = criteria.from( Product.class );
|
||||||
|
@ -957,10 +958,16 @@ public class DynamicFilterTest extends BaseNonConfigCoreFunctionalTestCase {
|
||||||
|
|
||||||
Product prod = result.get( 0 );
|
Product prod = result.get( 0 );
|
||||||
|
|
||||||
|
|
||||||
long initLoadCount = sessionFactory().getStatistics().getCollectionLoadCount();
|
long initLoadCount = sessionFactory().getStatistics().getCollectionLoadCount();
|
||||||
long initFetchCount = sessionFactory().getStatistics().getCollectionFetchCount();
|
long initFetchCount = sessionFactory().getStatistics().getCollectionFetchCount();
|
||||||
|
|
||||||
// should already have been initialized...
|
// categories should not have been initialized...
|
||||||
|
assertTrue(
|
||||||
|
"load with join fetch of many-to-many triggers join fetch",
|
||||||
|
( 0 == initLoadCount ) && ( 0 == initFetchCount )
|
||||||
|
);
|
||||||
|
|
||||||
int size = prod.getCategories().size();
|
int size = prod.getCategories().size();
|
||||||
assertEquals( "Incorrect non-filtered collection count", 2, size );
|
assertEquals( "Incorrect non-filtered collection count", 2, size );
|
||||||
|
|
||||||
|
@ -968,8 +975,8 @@ public class DynamicFilterTest extends BaseNonConfigCoreFunctionalTestCase {
|
||||||
long currFetchCount = sessionFactory().getStatistics().getCollectionFetchCount();
|
long currFetchCount = sessionFactory().getStatistics().getCollectionFetchCount();
|
||||||
|
|
||||||
assertTrue(
|
assertTrue(
|
||||||
"load with join fetch of many-to-many did not trigger join fetch",
|
"load with join fetch of many-to-many triggers join fetch",
|
||||||
( initLoadCount == currLoadCount ) && ( initFetchCount == currFetchCount )
|
( 1 == currLoadCount ) && ( 1 == currFetchCount )
|
||||||
);
|
);
|
||||||
|
|
||||||
// make sure we did not get back a collection of proxies
|
// make sure we did not get back a collection of proxies
|
||||||
|
|
Loading…
Reference in New Issue