Search in sources :

Example 36 with TableGroupJoin

use of org.hibernate.sql.ast.tree.from.TableGroupJoin in project hibernate-orm by hibernate.

the class BaseSqmToSqlAstConverter method buildFetch.

private Fetch buildFetch(NavigablePath fetchablePath, FetchParent fetchParent, Fetchable fetchable, FetchTiming fetchTiming, boolean joined, String alias) {
    try {
        final Fetch fetch = fetchParent.generateFetchableFetch(fetchable, fetchablePath, fetchTiming, joined, alias, this);
        if (fetchable instanceof PluralAttributeMapping && fetch.getTiming() == FetchTiming.IMMEDIATE && joined) {
            final TableGroup tableGroup = getFromClauseIndex().getTableGroup(fetchablePath);
            final PluralAttributeMapping pluralAttributeMapping = (PluralAttributeMapping) fetchable;
            final Joinable joinable = pluralAttributeMapping.getCollectionDescriptor().getCollectionType().getAssociatedJoinable(getCreationContext().getSessionFactory());
            joinable.applyBaseRestrictions((predicate) -> addCollectionFilterPredicate(tableGroup.getGroupAlias(), predicate), tableGroup, true, getLoadQueryInfluencers().getEnabledFilters(), null, this);
            pluralAttributeMapping.applyBaseManyToManyRestrictions((predicate) -> {
                final TableGroup parentTableGroup = getFromClauseIndex().getTableGroup(fetchParent.getNavigablePath());
                TableGroupJoin pluralTableGroupJoin = null;
                for (TableGroupJoin nestedTableGroupJoin : parentTableGroup.getTableGroupJoins()) {
                    if (nestedTableGroupJoin.getNavigablePath() == fetchablePath) {
                        pluralTableGroupJoin = nestedTableGroupJoin;
                        break;
                    }
                }
                assert pluralTableGroupJoin != null;
                pluralTableGroupJoin.applyPredicate(predicate);
            }, tableGroup, true, getLoadQueryInfluencers().getEnabledFilters(), null, this);
            if (currentQuerySpec().isRoot()) {
                assert tableGroup.getModelPart() == pluralAttributeMapping;
                applyOrdering(tableGroup, pluralAttributeMapping);
            }
        }
        return fetch;
    } catch (RuntimeException e) {
        throw new HibernateException(String.format(Locale.ROOT, "Could not generate fetch : %s -> %s", fetchParent.getNavigablePath(), fetchable.getFetchableName()), e);
    }
}
Also used : Fetch(org.hibernate.sql.results.graph.Fetch) TableGroupJoin(org.hibernate.sql.ast.tree.from.TableGroupJoin) VirtualTableGroup(org.hibernate.sql.ast.tree.from.VirtualTableGroup) LazyTableGroup(org.hibernate.sql.ast.tree.from.LazyTableGroup) TableGroup(org.hibernate.sql.ast.tree.from.TableGroup) CorrelatedTableGroup(org.hibernate.sql.ast.tree.from.CorrelatedTableGroup) CorrelatedPluralTableGroup(org.hibernate.sql.ast.tree.from.CorrelatedPluralTableGroup) PluralTableGroup(org.hibernate.sql.ast.tree.from.PluralTableGroup) QueryPartTableGroup(org.hibernate.sql.ast.tree.from.QueryPartTableGroup) HibernateException(org.hibernate.HibernateException) Joinable(org.hibernate.persister.entity.Joinable) PluralAttributeMapping(org.hibernate.metamodel.mapping.PluralAttributeMapping)

Example 37 with TableGroupJoin

use of org.hibernate.sql.ast.tree.from.TableGroupJoin in project hibernate-orm by hibernate.

the class SqlTreePrinter method logTableGroupDetails.

private void logTableGroupDetails(TableGroup tableGroup) {
    if (tableGroup instanceof LazyTableGroup) {
        TableGroup underlyingTableGroup = ((LazyTableGroup) tableGroup).getUnderlyingTableGroup();
        if (underlyingTableGroup != null) {
            logTableGroupDetails(underlyingTableGroup);
        }
        return;
    }
    if (tableGroup.getPrimaryTableReference() instanceof NamedTableReference) {
        logWithIndentation("primaryTableReference : %s as %s", tableGroup.getPrimaryTableReference().getTableId(), tableGroup.getPrimaryTableReference().getIdentificationVariable());
    } else {
        if (tableGroup.getPrimaryTableReference() instanceof ValuesTableReference) {
            logWithIndentation("primaryTableReference : values (..) as %s", tableGroup.getPrimaryTableReference().getIdentificationVariable());
        } else if (tableGroup.getPrimaryTableReference() instanceof FunctionTableReference) {
            logWithIndentation("primaryTableReference : %s(...) as %s", ((FunctionTableReference) tableGroup.getPrimaryTableReference()).getFunctionExpression().getFunctionName(), tableGroup.getPrimaryTableReference().getIdentificationVariable());
        } else {
            logNode("PrimaryTableReference as " + tableGroup.getPrimaryTableReference().getIdentificationVariable(), () -> {
                QueryPart queryPart = ((QueryPartTableReference) tableGroup.getPrimaryTableReference()).getQueryPart();
                visitQueryPart(queryPart);
            });
        }
    }
    final List<TableReferenceJoin> tableReferenceJoins = tableGroup.getTableReferenceJoins();
    if (!tableReferenceJoins.isEmpty()) {
        logNode("TableReferenceJoins", () -> {
            for (TableReferenceJoin join : tableReferenceJoins) {
                logWithIndentation("%s join %s as %s", join.getJoinType().getText(), join.getJoinedTableReference().getTableExpression(), join.getJoinedTableReference().getIdentificationVariable());
            }
        });
    }
    final List<TableGroupJoin> nestedTableGroupJoins = tableGroup.getNestedTableGroupJoins();
    if (!nestedTableGroupJoins.isEmpty()) {
        logNode("NestedTableGroupJoins", () -> tableGroup.visitNestedTableGroupJoins(this::visitTableGroupJoin));
    }
    final List<TableGroupJoin> tableGroupJoins = tableGroup.getTableGroupJoins();
    if (!tableGroupJoins.isEmpty()) {
        logNode("TableGroupJoins", () -> tableGroup.visitTableGroupJoins(this::visitTableGroupJoin));
    }
}
Also used : TableGroupJoin(org.hibernate.sql.ast.tree.from.TableGroupJoin) LazyTableGroup(org.hibernate.sql.ast.tree.from.LazyTableGroup) TableGroup(org.hibernate.sql.ast.tree.from.TableGroup) NamedTableReference(org.hibernate.sql.ast.tree.from.NamedTableReference) LazyTableGroup(org.hibernate.sql.ast.tree.from.LazyTableGroup) FunctionTableReference(org.hibernate.sql.ast.tree.from.FunctionTableReference) QueryPart(org.hibernate.sql.ast.tree.select.QueryPart) ValuesTableReference(org.hibernate.sql.ast.tree.from.ValuesTableReference) TableReferenceJoin(org.hibernate.sql.ast.tree.from.TableReferenceJoin)

Example 38 with TableGroupJoin

use of org.hibernate.sql.ast.tree.from.TableGroupJoin in project hibernate-orm by hibernate.

the class MappedFetchTests method baseline.

@Test
public void baseline(SessionFactoryScope scope) {
    final SessionFactoryImplementor sessionFactory = scope.getSessionFactory();
    final MappingMetamodel domainModel = sessionFactory.getRuntimeMetamodels().getMappingMetamodel();
    final EntityPersister rootEntityDescriptor = domainModel.getEntityDescriptor(RootEntity.class);
    final SelectStatement sqlAst = LoaderSelectBuilder.createSelect(rootEntityDescriptor, null, rootEntityDescriptor.getIdentifierMapping(), null, 1, LoadQueryInfluencers.NONE, LockOptions.NONE, jdbcParameter -> {
    }, sessionFactory);
    assertThat(sqlAst.getDomainResultDescriptors().size(), is(1));
    final DomainResult domainResult = sqlAst.getDomainResultDescriptors().get(0);
    assertThat(domainResult, instanceOf(EntityResult.class));
    final EntityResult entityResult = (EntityResult) domainResult;
    final List<Fetch> fetches = entityResult.getFetches();
    // name + both lists
    assertThat(fetches.size(), is(3));
    // order is alphabetical...
    final Fetch nameFetch = fetches.get(0);
    assertThat(nameFetch.getFetchedMapping().getFetchableName(), is("name"));
    assertThat(nameFetch, instanceOf(BasicFetch.class));
    final Fetch nickNamesFetch = fetches.get(1);
    assertThat(nickNamesFetch.getFetchedMapping().getFetchableName(), is("nickNames"));
    assertThat(nickNamesFetch, instanceOf(EagerCollectionFetch.class));
    final Fetch simpleEntitiesFetch = fetches.get(2);
    assertThat(simpleEntitiesFetch.getFetchedMapping().getFetchableName(), is("simpleEntities"));
    assertThat(simpleEntitiesFetch, instanceOf(DelayedCollectionFetch.class));
    final QuerySpec querySpec = sqlAst.getQuerySpec();
    final TableGroup tableGroup = querySpec.getFromClause().getRoots().get(0);
    assertThat(tableGroup.getModelPart(), is(rootEntityDescriptor));
    assertThat(tableGroup.getTableGroupJoins().size(), is(1));
    final TableGroupJoin collectionJoin = tableGroup.getTableGroupJoins().iterator().next();
    assertThat(collectionJoin.getJoinedGroup().getModelPart(), is(nickNamesFetch.getFetchedMapping()));
    assertThat(collectionJoin.getPredicate(), notNullValue());
    assertThat(collectionJoin.getPredicate(), instanceOf(ComparisonPredicate.class));
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) BasicFetch(org.hibernate.sql.results.graph.basic.BasicFetch) TableGroup(org.hibernate.sql.ast.tree.from.TableGroup) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) DomainResult(org.hibernate.sql.results.graph.DomainResult) EntityResult(org.hibernate.sql.results.graph.entity.EntityResult) ComparisonPredicate(org.hibernate.sql.ast.tree.predicate.ComparisonPredicate) DelayedCollectionFetch(org.hibernate.sql.results.graph.collection.internal.DelayedCollectionFetch) BasicFetch(org.hibernate.sql.results.graph.basic.BasicFetch) Fetch(org.hibernate.sql.results.graph.Fetch) EagerCollectionFetch(org.hibernate.sql.results.graph.collection.internal.EagerCollectionFetch) TableGroupJoin(org.hibernate.sql.ast.tree.from.TableGroupJoin) SelectStatement(org.hibernate.sql.ast.tree.select.SelectStatement) DelayedCollectionFetch(org.hibernate.sql.results.graph.collection.internal.DelayedCollectionFetch) MappingMetamodel(org.hibernate.metamodel.MappingMetamodel) QuerySpec(org.hibernate.sql.ast.tree.select.QuerySpec) EagerCollectionFetch(org.hibernate.sql.results.graph.collection.internal.EagerCollectionFetch) Test(org.junit.jupiter.api.Test)

Example 39 with TableGroupJoin

use of org.hibernate.sql.ast.tree.from.TableGroupJoin in project hibernate-orm by hibernate.

the class EntityJoinTest method testNoImpliedJoinGeneratedForEqualityComparison.

@Test
@TestForIssue(jiraKey = "HHH-11538")
public void testNoImpliedJoinGeneratedForEqualityComparison(SessionFactoryScope scope) {
    final String qry = "select r.id, cust.name " + "from FinancialRecord r " + "	join Customer cust on r.customer = cust" + "   order by r.id";
    scope.inTransaction((session) -> {
        final SessionFactoryImplementor factory = scope.getSessionFactory();
        final EntityMappingType customerEntityDescriptor = factory.getRuntimeMetamodels().getMappingMetamodel().findEntityDescriptor(Customer.class);
        final QueryEngine queryEngine = factory.getQueryEngine();
        final HqlTranslator hqlTranslator = queryEngine.getHqlTranslator();
        final SqmTranslatorFactory sqmTranslatorFactory = queryEngine.getSqmTranslatorFactory();
        final SqmStatement<Object> sqm = hqlTranslator.translate(qry);
        final SqmTranslator<SelectStatement> selectTranslator = sqmTranslatorFactory.createSelectTranslator((SqmSelectStatement<?>) sqm, QueryOptions.NONE, DomainParameterXref.empty(), QueryParameterBindings.NO_PARAM_BINDINGS, LoadQueryInfluencers.NONE, factory, true);
        final SqmTranslation<SelectStatement> sqmTranslation = selectTranslator.translate();
        final SelectStatement sqlAst = sqmTranslation.getSqlAst();
        final List<TableGroup> roots = sqlAst.getQuerySpec().getFromClause().getRoots();
        assertThat(roots.size(), is(1));
        final TableGroup rootTableGroup = roots.get(0);
        assertThat(rootTableGroup.getTableGroupJoins().size(), is(1));
        final TableGroupJoin tableGroupJoin = rootTableGroup.getTableGroupJoins().get(0);
        assertThat(tableGroupJoin.getJoinedGroup().getModelPart(), is(customerEntityDescriptor));
    });
}
Also used : TableGroup(org.hibernate.sql.ast.tree.from.TableGroup) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) QueryEngine(org.hibernate.query.spi.QueryEngine) TableGroupJoin(org.hibernate.sql.ast.tree.from.TableGroupJoin) SqmSelectStatement(org.hibernate.query.sqm.tree.select.SqmSelectStatement) SelectStatement(org.hibernate.sql.ast.tree.select.SelectStatement) HqlTranslator(org.hibernate.query.hql.HqlTranslator) EntityMappingType(org.hibernate.metamodel.mapping.EntityMappingType) SqmTranslatorFactory(org.hibernate.query.sqm.sql.SqmTranslatorFactory) Test(org.junit.jupiter.api.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Aggregations

TableGroupJoin (org.hibernate.sql.ast.tree.from.TableGroupJoin)39 TableGroup (org.hibernate.sql.ast.tree.from.TableGroup)34 LazyTableGroup (org.hibernate.sql.ast.tree.from.LazyTableGroup)17 PluralAttributeMapping (org.hibernate.metamodel.mapping.PluralAttributeMapping)14 PluralTableGroup (org.hibernate.sql.ast.tree.from.PluralTableGroup)14 NavigablePath (org.hibernate.query.spi.NavigablePath)13 SqlAstJoinType (org.hibernate.sql.ast.SqlAstJoinType)13 EntityPersister (org.hibernate.persister.entity.EntityPersister)11 CorrelatedTableGroup (org.hibernate.sql.ast.tree.from.CorrelatedTableGroup)11 QueryPartTableGroup (org.hibernate.sql.ast.tree.from.QueryPartTableGroup)10 TableGroupJoinProducer (org.hibernate.sql.ast.tree.from.TableGroupJoinProducer)10 VirtualTableGroup (org.hibernate.sql.ast.tree.from.VirtualTableGroup)10 Fetch (org.hibernate.sql.results.graph.Fetch)10 CorrelatedPluralTableGroup (org.hibernate.sql.ast.tree.from.CorrelatedPluralTableGroup)9 SelectStatement (org.hibernate.sql.ast.tree.select.SelectStatement)9 EntityValuedModelPart (org.hibernate.metamodel.mapping.EntityValuedModelPart)8 AbstractEntityPersister (org.hibernate.persister.entity.AbstractEntityPersister)8 DomainResult (org.hibernate.sql.results.graph.DomainResult)8 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)7 ModelPart (org.hibernate.metamodel.mapping.ModelPart)7