diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/entitygraph/ast/CriteriaEntityGraphTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/entitygraph/ast/CriteriaEntityGraphTest.java index f7844835e6..ba20d03360 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/entitygraph/ast/CriteriaEntityGraphTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/entitygraph/ast/CriteriaEntityGraphTest.java @@ -45,6 +45,7 @@ import org.hibernate.sql.ast.tree.from.StandardVirtualTableGroup; import org.hibernate.sql.ast.tree.from.TableGroup; import org.hibernate.sql.ast.tree.from.TableGroupJoin; import org.hibernate.sql.ast.tree.select.SelectStatement; +import org.hibernate.sql.results.graph.BiDirectionalFetch; import org.hibernate.sql.results.graph.DomainResult; import org.hibernate.sql.results.graph.Fetch; import org.hibernate.sql.results.graph.Fetchable; @@ -54,7 +55,6 @@ import org.hibernate.sql.results.graph.entity.EntityFetch; import org.hibernate.sql.results.graph.entity.EntityResult; import org.hibernate.sql.results.graph.entity.internal.EntityDelayedFetchImpl; import org.hibernate.sql.results.graph.entity.internal.EntityFetchJoinedImpl; -import org.hibernate.sql.results.graph.internal.ImmutableFetchList; import org.hibernate.testing.TestForIssue; import org.hibernate.testing.orm.junit.DomainModel; @@ -110,8 +110,20 @@ public class CriteriaEntityGraphTest implements SessionFactoryScopeAware { assertEmptyJoinedGroup( sqlAst ); // Check the domain-result graph - assertDomainResult( sqlAst, Cat.class, "owner", Person.class, - entityFetch -> assertThat( entityFetch, instanceOf( EntityDelayedFetchImpl.class ) ) + assertDomainResult( sqlAst, Cat.class, + fetch -> { + if (graphSemantic == GraphSemantic.LOAD) { + assertThat( fetch, instanceOf( BiDirectionalFetch.class ) ); + } else { + assertThat( fetch, instanceOf( EntityFetch.class ) ); + + final EntityFetch entityFetch = (EntityFetch) fetch; + assertThat( entityFetch.getFetchedMapping().getFetchableName(), is( "owner" ) ); + assertThat( entityFetch.getReferencedModePart().getJavaType().getJavaTypeClass(), assignableTo( Person.class ) ); + + assertThat(entityFetch, instanceOf(EntityDelayedFetchImpl.class)); + } + } ); } ); @@ -131,7 +143,14 @@ public class CriteriaEntityGraphTest implements SessionFactoryScopeAware { assertEntityValuedJoinedGroup( sqlAst, "owner", Person.class, this::assertPersonHomeAddressJoinedGroup ); // Check the domain-result graph - assertDomainResult( sqlAst, Cat.class, "owner", Person.class, entityFetch -> { + assertDomainResult( sqlAst, Cat.class, aFetch -> { + + assertThat( aFetch, instanceOf( EntityFetch.class ) ); + + final EntityFetch entityFetch = (EntityFetch) aFetch; + assertThat( entityFetch.getFetchedMapping().getFetchableName(), is( "owner" ) ); + assertThat( entityFetch.getReferencedModePart().getJavaType().getJavaTypeClass(), assignableTo( Person.class ) ); + if ( graphSemantic == GraphSemantic.LOAD ) { assertThat( entityFetch, instanceOf( EntityFetchJoinedImpl.class ) ); final EntityResult entityResult = ( (EntityFetchJoinedImpl) entityFetch ).getEntityResult(); @@ -175,7 +194,13 @@ public class CriteriaEntityGraphTest implements SessionFactoryScopeAware { } ); // Check the domain-result graph - assertDomainResult( sqlAst, Cat.class, "owner", Person.class, entityFetch -> { + assertDomainResult( sqlAst, Cat.class, aFetch -> { + assertThat( aFetch, instanceOf( EntityFetch.class ) ); + + final EntityFetch entityFetch = (EntityFetch) aFetch; + assertThat( entityFetch.getFetchedMapping().getFetchableName(), is( "owner" ) ); + assertThat( entityFetch.getReferencedModePart().getJavaType().getJavaTypeClass(), assignableTo( Person.class ) ); + assertThat( entityFetch, instanceOf( EntityFetchJoinedImpl.class ) ); final EntityResult ownerEntityResult = ( (EntityFetchJoinedImpl) entityFetch ).getEntityResult(); final Map> fetchClassByAttributeName = ownerEntityResult.getFetches() @@ -260,17 +285,8 @@ public class CriteriaEntityGraphTest implements SessionFactoryScopeAware { .next() .getJoinedGroup(); assertThat( compositeTableGroup, instanceOf( StandardVirtualTableGroup.class ) ); - assertThat( compositeTableGroup.getTableGroupJoins(), hasSize( 1 ) ); + assertThat( compositeTableGroup.getTableGroupJoins(), isEmpty() ); assertThat( compositeTableGroup.getNestedTableGroupJoins(), isEmpty() ); - - final TableGroup countryTableGroup = compositeTableGroup.getTableGroupJoins() - .iterator() - .next() - .getJoinedGroup(); - assertThat( countryTableGroup.getModelPart().getPartName(), is( "country" ) ); - - assertThat( countryTableGroup.getTableGroupJoins(), isEmpty() ); - assertThat( countryTableGroup.getNestedTableGroupJoins(), isEmpty() ); } else { assertThat( tableGroup.getTableGroupJoins(), isEmpty() ); @@ -338,9 +354,7 @@ public class CriteriaEntityGraphTest implements SessionFactoryScopeAware { private void assertDomainResult(SelectStatement sqlAst, Class expectedEntityJpaClass, - String expectedAttributeName, - Class expectedAttributeEntityJpaClass, - Consumer entityFetchConsumer) { + Consumer fetchConsumer) { assertThat( sqlAst.getDomainResultDescriptors(), hasSize( 1 ) ); final DomainResult domainResult = sqlAst.getDomainResultDescriptors().get( 0 ); @@ -351,13 +365,7 @@ public class CriteriaEntityGraphTest implements SessionFactoryScopeAware { assertThat( entityResult.getFetches().size(), is( 1 ) ); final Fetch fetch = entityResult.getFetches().iterator().next(); - assertThat( fetch, instanceOf( EntityFetch.class ) ); - - final EntityFetch entityFetch = (EntityFetch) fetch; - assertThat( entityFetch.getFetchedMapping().getFetchableName(), is( expectedAttributeName ) ); - assertThat( entityFetch.getReferencedModePart().getJavaType().getJavaTypeClass(), assignableTo( expectedAttributeEntityJpaClass ) ); - - entityFetchConsumer.accept( entityFetch ); + fetchConsumer.accept(fetch); } private SelectStatement buildSqlSelectAst( diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/entitygraph/ast/HqlEntityGraphTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/entitygraph/ast/HqlEntityGraphTest.java index 199922b277..01f5db3477 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/entitygraph/ast/HqlEntityGraphTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/entitygraph/ast/HqlEntityGraphTest.java @@ -43,6 +43,7 @@ import org.hibernate.sql.ast.tree.from.StandardVirtualTableGroup; import org.hibernate.sql.ast.tree.from.TableGroup; import org.hibernate.sql.ast.tree.from.TableGroupJoin; import org.hibernate.sql.ast.tree.select.SelectStatement; +import org.hibernate.sql.results.graph.BiDirectionalFetch; import org.hibernate.sql.results.graph.DomainResult; import org.hibernate.sql.results.graph.Fetch; import org.hibernate.sql.results.graph.Fetchable; @@ -107,8 +108,20 @@ public class HqlEntityGraphTest implements SessionFactoryScopeAware { assertEmptyJoinedGroup( sqlAst ); // Check the domain-result graph - assertDomainResult( sqlAst, Cat.class, "owner", Person.class, - entityFetch -> assertThat( entityFetch, instanceOf( EntityDelayedFetchImpl.class ) ) + assertDomainResult(sqlAst, Cat.class, + fetch -> { + if (graphSemantic == GraphSemantic.LOAD) { + assertThat(fetch, instanceOf(BiDirectionalFetch.class)); + } else { + assertThat(fetch, instanceOf(EntityFetch.class)); + + final EntityFetch entityFetch = (EntityFetch) fetch; + assertThat(entityFetch.getFetchedMapping().getFetchableName(), is("owner")); + assertThat(entityFetch.getReferencedModePart().getJavaType().getJavaTypeClass(), assignableTo(Person.class)); + + assertThat(entityFetch, instanceOf(EntityDelayedFetchImpl.class)); + } + } ); } ); @@ -128,12 +141,18 @@ public class HqlEntityGraphTest implements SessionFactoryScopeAware { assertEntityValuedJoinedGroup( sqlAst, "owner", Person.class, this::assertPersonHomeAddressJoinedGroup ); // Check the domain-result graph - assertDomainResult( sqlAst, Cat.class, "owner", Person.class, entityFetch -> { + assertDomainResult( sqlAst, Cat.class, fetch -> { + assertThat( fetch, instanceOf( EntityFetch.class ) ); + + final EntityFetch entityFetch = (EntityFetch) fetch; + assertThat( entityFetch.getFetchedMapping().getFetchableName(), is( "owner" ) ); + assertThat( entityFetch.getReferencedModePart().getJavaType().getJavaTypeClass(), assignableTo( Person.class ) ); + if ( graphSemantic == GraphSemantic.LOAD ) { assertThat( entityFetch, instanceOf( EntityFetchJoinedImpl.class ) ); final EntityResult entityResult = ( (EntityFetchJoinedImpl) entityFetch ).getEntityResult(); final Map> fetchClassByAttributeName = entityResult.getFetches().stream().collect( Collectors.toMap( - fetch -> fetch.getFetchedMapping().getPartName(), + aFetch -> aFetch.getFetchedMapping().getPartName(), Fetch::getClass ) ); final Map> expectedFetchClassByAttributeName = new HashMap<>(); @@ -172,12 +191,18 @@ public class HqlEntityGraphTest implements SessionFactoryScopeAware { } ); // Check the domain-result graph - assertDomainResult( sqlAst, Cat.class, "owner", Person.class, entityFetch -> { + assertDomainResult( sqlAst, Cat.class, fetch -> { + assertThat( fetch, instanceOf( EntityFetch.class ) ); + + final EntityFetch entityFetch = (EntityFetch) fetch; + assertThat( entityFetch.getFetchedMapping().getFetchableName(), is( "owner" ) ); + assertThat( entityFetch.getReferencedModePart().getJavaType().getJavaTypeClass(), assignableTo( Person.class ) ); + assertThat( entityFetch, instanceOf( EntityFetchJoinedImpl.class ) ); final EntityResult ownerEntityResult = ( (EntityFetchJoinedImpl) entityFetch ).getEntityResult(); final Map> fetchClassByAttributeName = ownerEntityResult.getFetches() .stream().collect( Collectors.toMap( - fetch -> fetch.getFetchedMapping().getPartName(), + aFetch -> aFetch.getFetchedMapping().getPartName(), Fetch::getClass ) ); final Map> expectedFetchClassByAttributeName = new HashMap<>(); @@ -258,17 +283,8 @@ public class HqlEntityGraphTest implements SessionFactoryScopeAware { .next() .getJoinedGroup(); assertThat( compositeTableGroup, instanceOf( StandardVirtualTableGroup.class ) ); - assertThat( compositeTableGroup.getTableGroupJoins(), hasSize( 1 ) ); + assertThat( compositeTableGroup.getTableGroupJoins(), isEmpty( ) ); assertThat( compositeTableGroup.getNestedTableGroupJoins(), isEmpty() ); - - final TableGroup countryTableGroup = compositeTableGroup.getTableGroupJoins() - .iterator() - .next() - .getJoinedGroup(); - assertThat( countryTableGroup.getModelPart().getPartName(), is( "country" ) ); - - assertThat( countryTableGroup.getTableGroupJoins(), isEmpty() ); - assertThat( countryTableGroup.getNestedTableGroupJoins(), isEmpty() ); } else { assertThat( tableGroup.getTableGroupJoins(), isEmpty() ); @@ -335,9 +351,7 @@ public class HqlEntityGraphTest implements SessionFactoryScopeAware { private void assertDomainResult(SelectStatement sqlAst, Class expectedEntityJpaClass, - String expectedAttributeName, - Class expectedAttributeEntityJpaClass, - Consumer entityFetchConsumer) { + Consumer fetchConsumer) { assertThat( sqlAst.getDomainResultDescriptors(), hasSize( 1 ) ); final DomainResult domainResult = sqlAst.getDomainResultDescriptors().get( 0 ); @@ -348,13 +362,7 @@ public class HqlEntityGraphTest implements SessionFactoryScopeAware { assertThat( entityResult.getFetches().size(), is( 1 ) ); final Fetch fetch = entityResult.getFetches().iterator().next(); - assertThat( fetch, instanceOf( EntityFetch.class ) ); - - final EntityFetch entityFetch = (EntityFetch) fetch; - assertThat( entityFetch.getFetchedMapping().getFetchableName(), is( expectedAttributeName ) ); - assertThat( entityFetch.getReferencedModePart().getJavaType().getJavaTypeClass(), assignableTo( expectedAttributeEntityJpaClass ) ); - - entityFetchConsumer.accept( entityFetch ); + fetchConsumer.accept(fetch); } private SelectStatement buildSqlSelectAst( diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/graphs/LoadAndFetchGraphTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/graphs/LoadAndFetchGraphTest.java index cb25764bef..46784350e2 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/graphs/LoadAndFetchGraphTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/graphs/LoadAndFetchGraphTest.java @@ -236,8 +236,7 @@ public class LoadAndFetchGraphTest extends BaseEntityManagerFunctionalTestCase { // depending on the mapping. assertTrue( Hibernate.isInitialized( cEntity.getEagerC() ) ); - // 1 because contrary to 5.x we fetch the eager C along with the main query - assertEquals( 1L, statistics.getPrepareStatementCount() ); + assertEquals( 2L, statistics.getPrepareStatementCount() ); } ); } @@ -340,8 +339,7 @@ public class LoadAndFetchGraphTest extends BaseEntityManagerFunctionalTestCase { assertTrue( Hibernate.isInitialized( cEntity.getEagerC() ) ); } - // 1 because contrary to 5.x we fetch the eager C along with the main query - assertEquals( 1L, statistics.getPrepareStatementCount() ); + assertEquals( 3L, statistics.getPrepareStatementCount() ); } ); }