use of org.hibernate.sql.ast.tree.select.SelectStatement in project hibernate-orm by hibernate.
the class CriteriaEntityGraphTest method testBasicElementCollections.
@ParameterizedTest
@EnumSource(GraphSemantic.class)
void testBasicElementCollections(GraphSemantic graphSemantic) {
scope.inTransaction(session -> {
final RootGraphImplementor<Dog> eg = session.createEntityGraph(Dog.class);
eg.addAttributeNodes("favorites");
final SelectStatement sqlAst = buildSqlSelectAst(Dog.class, eg, graphSemantic, session);
// 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 CriteriaEntityGraphTest method testEmbeddedCollection.
@ParameterizedTest
@EnumSource(GraphSemantic.class)
void testEmbeddedCollection(GraphSemantic graphSemantic) {
scope.inTransaction(session -> {
final RootGraphImplementor<ExpressCompany> eg = session.createEntityGraph(ExpressCompany.class);
eg.addAttributeNodes("shipAddresses");
final SelectStatement sqlAst = buildSqlSelectAst(ExpressCompany.class, eg, graphSemantic, session);
// Check the from-clause
assertPluralAttributeJoinedGroup(sqlAst, "shipAddresses", tableGroup -> {
if (graphSemantic == GraphSemantic.LOAD) {
assertThat(tableGroup.getTableGroupJoins(), isEmpty());
assertThat(tableGroup.getNestedTableGroupJoins(), hasSize(1));
final TableGroup compositeTableGroup = tableGroup.getNestedTableGroupJoins().iterator().next().getJoinedGroup();
assertThat(compositeTableGroup, instanceOf(StandardVirtualTableGroup.class));
assertThat(compositeTableGroup.getTableGroupJoins(), hasSize(1));
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());
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 CriteriaEntityGraphTest method buildSqlSelectAst.
private <T> SelectStatement buildSqlSelectAst(Class<T> entityType, RootGraphImplementor<T> entityGraph, GraphSemantic mode, SessionImplementor session) {
final LoadQueryInfluencers loadQueryInfluencers = new LoadQueryInfluencers(session.getSessionFactory());
final CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();
CriteriaQuery<T> criteriaQuery = criteriaBuilder.createQuery(entityType);
criteriaQuery.select(criteriaQuery.from(entityType));
final QueryImplementor<T> query = session.createQuery(criteriaQuery);
final SqmQueryImplementor<String> hqlQuery = (SqmQueryImplementor<String>) query;
hqlQuery.applyGraph(entityGraph, mode);
final SqmSelectStatement<String> sqmStatement = (SqmSelectStatement<String>) hqlQuery.getSqmStatement();
final StandardSqmTranslator<SelectStatement> sqmConverter = new StandardSqmTranslator<>(sqmStatement, hqlQuery.getQueryOptions(), ((QuerySqmImpl<?>) hqlQuery).getDomainParameterXref(), query.getParameterBindings(), loadQueryInfluencers, session.getSessionFactory(), true);
final SqmTranslation<SelectStatement> sqmInterpretation = sqmConverter.translate();
return sqmInterpretation.getSqlAst();
}
use of org.hibernate.sql.ast.tree.select.SelectStatement in project hibernate-orm by hibernate.
the class CriteriaEntityGraphTest 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, 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);
List<Fetch> fetches = ownerEntityResult.getFetches();
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));
});
});
}
use of org.hibernate.sql.ast.tree.select.SelectStatement in project hibernate-orm by hibernate.
the class EntityGraphLoadPlanBuilderTest method testFetchLoadPlanBuildingWithDeepSubgraph.
@Test
void testFetchLoadPlanBuildingWithDeepSubgraph() {
scope.inTransaction(em -> {
final RootGraphImplementor<Cat> eg = em.createEntityGraph(Cat.class);
eg.addSubgraph("owner", Person.class).addSubgraph("company", ExpressCompany.class);
final SelectStatement sqlAst = buildSqlSelectAst(Cat.class, eg, GraphSemantic.FETCH, scope);
// 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