use of org.hibernate.sql.ast.tree.select.SelectStatement in project hibernate-orm by hibernate.
the class EntityGraphLoadPlanBuilderTest method testEmbeddedCollectionLoadGraph.
@ParameterizedTest
@EnumSource(GraphSemantic.class)
void testEmbeddedCollectionLoadGraph(GraphSemantic graphSemantic) {
scope.inTransaction(em -> {
final RootGraphImplementor<ExpressCompany> eg = em.createEntityGraph(ExpressCompany.class);
eg.addAttributeNodes("shipAddresses");
final SelectStatement sqlAst = buildSqlSelectAst(ExpressCompany.class, eg, graphSemantic, scope);
// Check the from-clause
assertPluralAttributeJoinedGroup(sqlAst, "shipAddresses", tableGroup -> {
if (graphSemantic == GraphSemantic.LOAD) {
assertThat(tableGroup.getTableGroupJoins(), isEmpty());
assertThat(tableGroup.getNestedTableGroupJoins(), hasSize(1));
final TableGroup compositeTableGroup = CollectionUtils.getOnlyElement(tableGroup.getNestedTableGroupJoins()).getJoinedGroup();
assertThat(compositeTableGroup, instanceOf(StandardVirtualTableGroup.class));
assertThat(compositeTableGroup.getTableGroupJoins(), hasSize(1));
assertThat(compositeTableGroup.getNestedTableGroupJoins(), isEmpty());
final TableGroup countryTableGroup = CollectionUtils.getOnlyElement(compositeTableGroup.getTableGroupJoins()).getJoinedGroup();
assertThat(countryTableGroup.getModelPart().getPartName(), is("country"));
assertThat(countryTableGroup.getTableGroupJoins(), isEmpty());
assertThat(countryTableGroup.getNestedTableGroupJoins(), isEmpty());
} else {
assertThat(tableGroup.getTableGroupJoins(), isEmpty());
assertThat(tableGroup.getNestedTableGroupJoins(), hasSize(1));
final TableGroup compositeTableGroup = CollectionUtils.getOnlyElement(tableGroup.getNestedTableGroupJoins()).getJoinedGroup();
assertThat(compositeTableGroup, instanceOf(StandardVirtualTableGroup.class));
assertThat(compositeTableGroup.getTableGroupJoins(), isEmpty());
assertThat(compositeTableGroup.getNestedTableGroupJoins(), isEmpty());
}
});
});
}
use of org.hibernate.sql.ast.tree.select.SelectStatement in project hibernate-orm by hibernate.
the class EntityGraphLoadPlanBuilderTest method testLoadPlanBuildingWithSubgraph.
@ParameterizedTest
@EnumSource(GraphSemantic.class)
void testLoadPlanBuildingWithSubgraph(GraphSemantic graphSemantic) {
scope.inTransaction(em -> {
final RootGraphImplementor<Cat> eg = em.createEntityGraph(Cat.class);
eg.addSubgraph("owner", Person.class);
final SelectStatement sqlAst = buildSqlSelectAst(Cat.class, eg, graphSemantic, scope);
// Check the from-clause
assertEntityValuedJoinedGroup(sqlAst, "owner", Person.class, this::assertPersonHomeAddressJoinedGroup);
// Check the domain-result graph
assertDomainResult(sqlAst, Cat.class, "owner", Person.class, entityFetch -> {
if (graphSemantic == GraphSemantic.LOAD) {
assertThat(entityFetch, instanceOf(EntityFetchJoinedImpl.class));
final EntityResult entityResult = ((EntityFetchJoinedImpl) entityFetch).getEntityResult();
final Map<String, Class<? extends Fetch>> fetchClassByAttributeName = entityResult.getFetches().stream().collect(Collectors.toMap(fetch -> fetch.getFetchedMapping().getPartName(), Fetch::getClass));
final Map<String, Class<? extends Fetch>> expectedFetchClassByAttributeName = new HashMap<>();
expectedFetchClassByAttributeName.put("pets", DelayedCollectionFetch.class);
expectedFetchClassByAttributeName.put("homeAddress", EmbeddableFetchImpl.class);
expectedFetchClassByAttributeName.put("company", EntityDelayedFetchImpl.class);
assertThat(fetchClassByAttributeName, is(expectedFetchClassByAttributeName));
}
});
});
}
use of org.hibernate.sql.ast.tree.select.SelectStatement in project hibernate-orm by hibernate.
the class EntityGraphLoadPlanBuilderTest method testBasicElementCollections.
@ParameterizedTest
@EnumSource(GraphSemantic.class)
void testBasicElementCollections(GraphSemantic graphSemantic) {
scope.inTransaction(em -> {
final RootGraphImplementor<Dog> eg = em.createEntityGraph(Dog.class);
eg.addAttributeNodes("favorites");
final SelectStatement sqlAst = buildSqlSelectAst(Dog.class, eg, graphSemantic, scope);
// Check the from-clause
assertPluralAttributeJoinedGroup(sqlAst, "favorites", tableGroup -> {
});
});
}
use of org.hibernate.sql.ast.tree.select.SelectStatement in project hibernate-orm by hibernate.
the class HqlEntityGraphTest method testBasicSemantics.
@ParameterizedTest
@EnumSource(GraphSemantic.class)
void testBasicSemantics(GraphSemantic graphSemantic) {
scope.inTransaction(session -> {
final RootGraphImplementor<Cat> eg = session.createEntityGraph(Cat.class);
final SelectStatement sqlAst = buildSqlSelectAst(Cat.class, "select c from Cat c", eg, graphSemantic, session);
// Check the from-clause
assertEmptyJoinedGroup(sqlAst);
// Check the domain-result graph
assertDomainResult(sqlAst, Cat.class, "owner", Person.class, entityFetch -> assertThat(entityFetch, instanceOf(EntityDelayedFetchImpl.class)));
});
}
use of org.hibernate.sql.ast.tree.select.SelectStatement in project hibernate-orm by hibernate.
the class HqlEntityGraphTest method testFetchSemanticsWithDeepSubgraph.
@Test
void testFetchSemanticsWithDeepSubgraph() {
scope.inTransaction(session -> {
final RootGraphImplementor<Cat> eg = session.createEntityGraph(Cat.class);
eg.addSubgraph("owner", Person.class).addSubgraph("company", ExpressCompany.class);
final SelectStatement sqlAst = buildSqlSelectAst(Cat.class, "select c from Cat as c", eg, GraphSemantic.FETCH, session);
// Check the from-clause
assertEntityValuedJoinedGroup(sqlAst, "owner", Person.class, tableGroup -> {
List<TableGroupJoin> tableGroupJoins = tableGroup.getTableGroupJoins();
Map<String, Class<? extends TableGroup>> tableGroupByName = tableGroupJoins.stream().map(TableGroupJoin::getJoinedGroup).collect(Collectors.toMap(tg -> tg.getModelPart().getPartName(), TableGroup::getClass));
Map<String, Class<? extends TableGroup>> expectedTableGroupByName = new HashMap<>();
expectedTableGroupByName.put("homeAddress", StandardVirtualTableGroup.class);
expectedTableGroupByName.put("company", LazyTableGroup.class);
assertThat(tableGroupByName, is(expectedTableGroupByName));
});
// Check the domain-result graph
assertDomainResult(sqlAst, Cat.class, "owner", Person.class, entityFetch -> {
assertThat(entityFetch, instanceOf(EntityFetchJoinedImpl.class));
final EntityResult ownerEntityResult = ((EntityFetchJoinedImpl) entityFetch).getEntityResult();
final Map<String, Class<? extends Fetch>> fetchClassByAttributeName = ownerEntityResult.getFetches().stream().collect(Collectors.toMap(fetch -> fetch.getFetchedMapping().getPartName(), Fetch::getClass));
final Map<String, Class<? extends Fetch>> expectedFetchClassByAttributeName = new HashMap<>();
expectedFetchClassByAttributeName.put("homeAddress", EmbeddableFetchImpl.class);
expectedFetchClassByAttributeName.put("pets", DelayedCollectionFetch.class);
expectedFetchClassByAttributeName.put("company", EntityFetchJoinedImpl.class);
assertThat(fetchClassByAttributeName, is(expectedFetchClassByAttributeName));
Fetchable fetchable = getFetchable("company", Person.class);
final Fetch companyFetch = ownerEntityResult.findFetch(fetchable);
assertThat(companyFetch, notNullValue());
final EntityResult companyEntityResult = ((EntityFetchJoinedImpl) companyFetch).getEntityResult();
assertThat(companyEntityResult.getFetches(), hasSize(1));
final Fetch shipAddressesFetch = companyEntityResult.getFetches().get(0);
assertThat(shipAddressesFetch.getFetchedMapping().getPartName(), is("shipAddresses"));
assertThat(shipAddressesFetch, instanceOf(DelayedCollectionFetch.class));
});
});
}
Aggregations