Search in sources :

Example 1 with CollectionDomainResult

use of org.hibernate.sql.results.graph.collection.internal.CollectionDomainResult in project hibernate-orm by hibernate.

the class LoadPlanBuilderTest method testCollectionInitializerCase.

@Test
public void testCollectionInitializerCase(SessionFactoryScope scope) {
    final SessionFactoryImplementor sessionFactory = scope.getSessionFactory();
    final EntityPersister posterEntityDescriptor = sessionFactory.getRuntimeMetamodels().getMappingMetamodel().getEntityDescriptor(Poster.class);
    final PluralAttributeMapping messages = (PluralAttributeMapping) posterEntityDescriptor.findAttributeMapping("messages");
    final CollectionLoaderSingleKey loader = new CollectionLoaderSingleKey(messages, LoadQueryInfluencers.NONE, sessionFactory);
    assertThat(loader.getSqlAst().getDomainResultDescriptors()).hasSize(1);
    assertThat(loader.getSqlAst().getDomainResultDescriptors().get(0)).isInstanceOf(CollectionDomainResult.class);
    final CollectionDomainResult domainResult = (CollectionDomainResult) loader.getSqlAst().getDomainResultDescriptors().get(0);
    DomainResultGraphPrinter.logDomainResultGraph(loader.getSqlAst().getDomainResultDescriptors());
    assertThat(domainResult.getFetches()).isEmpty();
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) CollectionDomainResult(org.hibernate.sql.results.graph.collection.internal.CollectionDomainResult) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) PluralAttributeMapping(org.hibernate.metamodel.mapping.PluralAttributeMapping) CollectionLoaderSingleKey(org.hibernate.loader.ast.internal.CollectionLoaderSingleKey) Test(org.junit.jupiter.api.Test)

Example 2 with CollectionDomainResult

use of org.hibernate.sql.results.graph.collection.internal.CollectionDomainResult in project hibernate-orm by hibernate.

the class PluralAttributeMappingImpl method createDomainResult.

@Override
public <T> DomainResult<T> createDomainResult(NavigablePath navigablePath, TableGroup tableGroup, String resultVariable, DomainResultCreationState creationState) {
    final TableGroup collectionTableGroup = creationState.getSqlAstCreationState().getFromClauseAccess().getTableGroup(navigablePath);
    assert collectionTableGroup != null;
    // This is only used for collection initialization where we know the owner is available, so we mark it as visited
    // which will cause bidirectional to-one associations to be treated as such and avoid a join
    creationState.registerVisitedAssociationKey(fkDescriptor.getAssociationKey());
    // noinspection unchecked
    return new CollectionDomainResult(navigablePath, this, resultVariable, tableGroup, creationState);
}
Also used : CollectionDomainResult(org.hibernate.sql.results.graph.collection.internal.CollectionDomainResult) CollectionTableGroup(org.hibernate.sql.ast.tree.from.CollectionTableGroup) OneToManyTableGroup(org.hibernate.sql.ast.tree.from.OneToManyTableGroup) TableGroup(org.hibernate.sql.ast.tree.from.TableGroup)

Example 3 with CollectionDomainResult

use of org.hibernate.sql.results.graph.collection.internal.CollectionDomainResult in project hibernate-orm by hibernate.

the class LoaderSelectBuilder method generateSelect.

private SelectStatement generateSelect(SubselectFetch subselect) {
    assert loadable instanceof PluralAttributeMapping;
    final PluralAttributeMapping attributeMapping = (PluralAttributeMapping) loadable;
    final QuerySpec rootQuerySpec = new QuerySpec(true);
    final NavigablePath rootNavigablePath = new NavigablePath(loadable.getRootPathName());
    // We need to initialize the acronymMap based on subselect.getLoadingSqlAst() to avoid alias collisions
    final Map<String, TableReference> tableReferences = AliasCollector.getTableReferences(subselect.getLoadingSqlAst());
    final LoaderSqlAstCreationState sqlAstCreationState = new LoaderSqlAstCreationState(rootQuerySpec, new SqlAliasBaseManager(tableReferences.keySet()), new SimpleFromClauseAccessImpl(), lockOptions, this::visitFetches, numberOfKeysToLoad > 1, creationContext);
    final TableGroup rootTableGroup = loadable.createRootTableGroup(true, rootNavigablePath, null, () -> rootQuerySpec::applyPredicate, sqlAstCreationState, creationContext);
    rootQuerySpec.getFromClause().addRoot(rootTableGroup);
    sqlAstCreationState.getFromClauseAccess().registerTableGroup(rootNavigablePath, rootTableGroup);
    registerPluralTableGroupParts(sqlAstCreationState.getFromClauseAccess(), rootTableGroup);
    // generate and apply the restriction
    applySubSelectRestriction(rootQuerySpec, rootNavigablePath, rootTableGroup, subselect, sqlAstCreationState);
    // NOTE : no need to check - we are explicitly processing a plural-attribute
    applyFiltering(rootQuerySpec, rootTableGroup, attributeMapping, sqlAstCreationState);
    applyOrdering(rootTableGroup, attributeMapping);
    return new SelectStatement(rootQuerySpec, List.of(new CollectionDomainResult(rootNavigablePath, attributeMapping, null, rootTableGroup, sqlAstCreationState)));
}
Also used : NavigablePath(org.hibernate.query.spi.NavigablePath) EntityIdentifierNavigablePath(org.hibernate.query.sqm.spi.EntityIdentifierNavigablePath) TableGroup(org.hibernate.sql.ast.tree.from.TableGroup) PluralTableGroup(org.hibernate.sql.ast.tree.from.PluralTableGroup) PluralAttributeMapping(org.hibernate.metamodel.mapping.PluralAttributeMapping) SimpleFromClauseAccessImpl(org.hibernate.sql.ast.spi.SimpleFromClauseAccessImpl) SqlAliasBaseManager(org.hibernate.sql.ast.spi.SqlAliasBaseManager) CollectionDomainResult(org.hibernate.sql.results.graph.collection.internal.CollectionDomainResult) SelectStatement(org.hibernate.sql.ast.tree.select.SelectStatement) TableReference(org.hibernate.sql.ast.tree.from.TableReference) QuerySpec(org.hibernate.sql.ast.tree.select.QuerySpec)

Aggregations

CollectionDomainResult (org.hibernate.sql.results.graph.collection.internal.CollectionDomainResult)3 PluralAttributeMapping (org.hibernate.metamodel.mapping.PluralAttributeMapping)2 TableGroup (org.hibernate.sql.ast.tree.from.TableGroup)2 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)1 CollectionLoaderSingleKey (org.hibernate.loader.ast.internal.CollectionLoaderSingleKey)1 EntityPersister (org.hibernate.persister.entity.EntityPersister)1 NavigablePath (org.hibernate.query.spi.NavigablePath)1 EntityIdentifierNavigablePath (org.hibernate.query.sqm.spi.EntityIdentifierNavigablePath)1 SimpleFromClauseAccessImpl (org.hibernate.sql.ast.spi.SimpleFromClauseAccessImpl)1 SqlAliasBaseManager (org.hibernate.sql.ast.spi.SqlAliasBaseManager)1 CollectionTableGroup (org.hibernate.sql.ast.tree.from.CollectionTableGroup)1 OneToManyTableGroup (org.hibernate.sql.ast.tree.from.OneToManyTableGroup)1 PluralTableGroup (org.hibernate.sql.ast.tree.from.PluralTableGroup)1 TableReference (org.hibernate.sql.ast.tree.from.TableReference)1 QuerySpec (org.hibernate.sql.ast.tree.select.QuerySpec)1 SelectStatement (org.hibernate.sql.ast.tree.select.SelectStatement)1 Test (org.junit.jupiter.api.Test)1