Search in sources :

Example 46 with SelectStatement

use of org.hibernate.sql.ast.tree.select.SelectStatement in project hibernate-orm by hibernate.

the class HqlEntityGraphTest 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, "select d from Dog as d", eg, graphSemantic, session);
        // Check the from-clause
        assertPluralAttributeJoinedGroup(sqlAst, "favorites", tableGroup -> {
        });
    });
}
Also used : SqmSelectStatement(org.hibernate.query.sqm.tree.select.SqmSelectStatement) SelectStatement(org.hibernate.sql.ast.tree.select.SelectStatement) EnumSource(org.junit.jupiter.params.provider.EnumSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 47 with SelectStatement

use of org.hibernate.sql.ast.tree.select.SelectStatement in project hibernate-orm by hibernate.

the class HqlEntityGraphTest method buildSqlSelectAst.

private <T> SelectStatement buildSqlSelectAst(Class<T> entityType, String hql, RootGraphImplementor<T> entityGraph, GraphSemantic mode, SessionImplementor session) {
    final LoadQueryInfluencers loadQueryInfluencers = new LoadQueryInfluencers(session.getSessionFactory());
    final QueryImplementor<T> query = session.createQuery(hql, entityType);
    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();
}
Also used : LoadQueryInfluencers(org.hibernate.engine.spi.LoadQueryInfluencers) SqmSelectStatement(org.hibernate.query.sqm.tree.select.SqmSelectStatement) SqmSelectStatement(org.hibernate.query.sqm.tree.select.SqmSelectStatement) SelectStatement(org.hibernate.sql.ast.tree.select.SelectStatement) SqmQueryImplementor(org.hibernate.query.hql.spi.SqmQueryImplementor) StandardSqmTranslator(org.hibernate.query.sqm.sql.internal.StandardSqmTranslator)

Example 48 with SelectStatement

use of org.hibernate.sql.ast.tree.select.SelectStatement 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 49 with SelectStatement

use of org.hibernate.sql.ast.tree.select.SelectStatement 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, null);
        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)

Example 50 with SelectStatement

use of org.hibernate.sql.ast.tree.select.SelectStatement in project hibernate-orm by hibernate.

the class AbstractResultTests method interpret.

protected SelectStatement interpret(String hql, QueryParameterBindings parameterBindings, SessionFactoryImplementor sessionFactory) {
    final QueryEngine queryEngine = sessionFactory.getQueryEngine();
    final SqmSelectStatement<?> sqm = (SqmSelectStatement<?>) queryEngine.getHqlTranslator().translate(hql, null);
    final SqmTranslatorFactory sqmTranslatorFactory = queryEngine.getSqmTranslatorFactory();
    final SqmTranslator<SelectStatement> sqmConverter = sqmTranslatorFactory.createSelectTranslator(sqm, QueryOptions.NONE, DomainParameterXref.from(sqm), parameterBindings, LoadQueryInfluencers.NONE, sessionFactory, true);
    return sqmConverter.translate().getSqlAst();
}
Also used : SqmSelectStatement(org.hibernate.query.sqm.tree.select.SqmSelectStatement) SelectStatement(org.hibernate.sql.ast.tree.select.SelectStatement) SqmSelectStatement(org.hibernate.query.sqm.tree.select.SqmSelectStatement) QueryEngine(org.hibernate.query.spi.QueryEngine) SqmTranslatorFactory(org.hibernate.query.sqm.sql.SqmTranslatorFactory)

Aggregations

SelectStatement (org.hibernate.sql.ast.tree.select.SelectStatement)51 TableGroup (org.hibernate.sql.ast.tree.from.TableGroup)26 ArrayList (java.util.ArrayList)20 SqmSelectStatement (org.hibernate.query.sqm.tree.select.SqmSelectStatement)18 JdbcSelect (org.hibernate.sql.exec.spi.JdbcSelect)16 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)15 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)15 EnumSource (org.junit.jupiter.params.provider.EnumSource)15 JdbcServices (org.hibernate.engine.jdbc.spi.JdbcServices)14 PluralAttributeMapping (org.hibernate.metamodel.mapping.PluralAttributeMapping)14 JdbcParameterBindings (org.hibernate.sql.exec.spi.JdbcParameterBindings)14 List (java.util.List)13 JdbcParameter (org.hibernate.sql.ast.tree.expression.JdbcParameter)13 DomainResult (org.hibernate.sql.results.graph.DomainResult)13 Test (org.junit.jupiter.api.Test)12 TableGroupJoin (org.hibernate.sql.ast.tree.from.TableGroupJoin)11 QuerySpec (org.hibernate.sql.ast.tree.select.QuerySpec)11 Map (java.util.Map)9 EntityPersister (org.hibernate.persister.entity.EntityPersister)9 LoadQueryInfluencers (org.hibernate.engine.spi.LoadQueryInfluencers)8