Search in sources :

Example 11 with SqmSelectStatement

use of org.hibernate.query.sqm.tree.select.SqmSelectStatement in project hibernate-orm by hibernate.

the class BaseSqmToSqlAstConverter method visitSelectStatement.

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Select statement
@Override
public SelectStatement visitSelectStatement(SqmSelectStatement<?> statement) {
    final CteContainer cteContainer = this.visitCteContainer(statement);
    final QueryPart queryPart = visitQueryPart(statement.getQueryPart());
    final List<DomainResult<?>> domainResults = queryPart.isRoot() ? this.domainResults : Collections.emptyList();
    return new SelectStatement(cteContainer, queryPart, domainResults);
}
Also used : SqmCteContainer(org.hibernate.query.sqm.tree.cte.SqmCteContainer) CteContainer(org.hibernate.sql.ast.tree.cte.CteContainer) SelectStatement(org.hibernate.sql.ast.tree.select.SelectStatement) SqmInsertSelectStatement(org.hibernate.query.sqm.tree.insert.SqmInsertSelectStatement) SqmSelectStatement(org.hibernate.query.sqm.tree.select.SqmSelectStatement) SqmQueryPart(org.hibernate.query.sqm.tree.select.SqmQueryPart) QueryPart(org.hibernate.sql.ast.tree.select.QueryPart) DomainResult(org.hibernate.sql.results.graph.DomainResult)

Example 12 with SqmSelectStatement

use of org.hibernate.query.sqm.tree.select.SqmSelectStatement in project hibernate-orm by hibernate.

the class AbstractSharedSessionContract method createSelectionQuery.

@Override
public SelectionQuery<?> createSelectionQuery(String hqlString) {
    checkOpen();
    pulseTransactionCoordinator();
    delayedAfterCompletion();
    try {
        final QueryEngine queryEngine = getFactory().getQueryEngine();
        final QueryInterpretationCache interpretationCache = queryEngine.getInterpretationCache();
        final HqlInterpretation hqlInterpretation = interpretationCache.resolveHqlInterpretation(hqlString, s -> queryEngine.getHqlTranslator().translate(hqlString));
        if (!(hqlInterpretation.getSqmStatement() instanceof SqmSelectStatement)) {
            throw new IllegalSelectQueryException("Expecting a selection query, but found `" + hqlString + "`", hqlString);
        }
        final SqmSelectionQuery<?> query = new SqmSelectionQueryImpl<>(hqlString, hqlInterpretation, this);
        query.setComment(hqlString);
        applyQuerySettingsAndHints(query);
        return query;
    } catch (RuntimeException e) {
        markForRollbackOnly();
        throw e;
    }
}
Also used : SqmSelectStatement(org.hibernate.query.sqm.tree.select.SqmSelectStatement) IllegalSelectQueryException(org.hibernate.query.IllegalSelectQueryException) QueryInterpretationCache(org.hibernate.query.spi.QueryInterpretationCache) HqlInterpretation(org.hibernate.query.spi.HqlInterpretation) SqmSelectionQueryImpl(org.hibernate.query.sqm.internal.SqmSelectionQueryImpl) QueryEngine(org.hibernate.query.spi.QueryEngine)

Example 13 with SqmSelectStatement

use of org.hibernate.query.sqm.tree.select.SqmSelectStatement 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 14 with SqmSelectStatement

use of org.hibernate.query.sqm.tree.select.SqmSelectStatement in project hibernate-orm by hibernate.

the class ASTParserLoadingTest method testQueryMetadataRetrievalWithFetching.

@Test
@TestForIssue(jiraKey = "HHH-1464")
public void testQueryMetadataRetrievalWithFetching() {
    // HHH-1464 : there was a problem due to the fact they we polled
    // the shallow version of the query plan to get the metadata.
    inSession(session -> {
        final Query query = session.createQuery("from Animal a inner join fetch a.mother");
        final SqmSelectStatement<?> sqmStatement = (SqmSelectStatement<?>) query.unwrap(QuerySqmImpl.class).getSqmStatement();
        assertEquals(1, sqmStatement.getQuerySpec().getSelectClause().getSelections().size());
        final SqmSelection<?> selection = sqmStatement.getQuerySpec().getSelectClause().getSelections().get(0);
        final SqmExpressible<?> selectionType = selection.getSelectableNode().getNodeType();
        assertThat(selectionType, instanceOf(EntityDomainType.class));
        assertThat(selectionType.getExpressibleJavaType().getJavaTypeClass(), equalTo(Animal.class));
    });
    Session s = openSession();
    s.close();
}
Also used : SqmSelectStatement(org.hibernate.query.sqm.tree.select.SqmSelectStatement) Query(org.hibernate.query.Query) EntityDomainType(org.hibernate.metamodel.model.domain.EntityDomainType) Session(org.hibernate.Session) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 15 with SqmSelectStatement

use of org.hibernate.query.sqm.tree.select.SqmSelectStatement in project hibernate-orm by hibernate.

the class ASTParserLoadingTest method testAliases.

@Test
public void testAliases() {
    Session s = openSession();
    Transaction t = s.beginTransaction();
    Animal a = new Animal();
    a.setBodyWeight(12.4f);
    a.setDescription("an animal");
    s.persist(a);
    Query<?> q = s.createQuery("select a.bodyWeight as abw, a.description from Animal a");
    SqmSelectStatement<?> sqmStatement = (SqmSelectStatement<?>) q.unwrap(QuerySqmImpl.class).getSqmStatement();
    List<SqmSelection<?>> selections = sqmStatement.getQuerySpec().getSelectClause().getSelections();
    assertThat(selections.size(), is(2));
    assertThat(selections.get(0).getAlias(), is("abw"));
    assertThat(selections.get(1).getAlias(), nullValue());
    q = s.createQuery("select count(*), avg(a.bodyWeight) as avg from Animal a");
    sqmStatement = (SqmSelectStatement<?>) q.unwrap(QuerySqmImpl.class).getSqmStatement();
    selections = sqmStatement.getQuerySpec().getSelectClause().getSelections();
    assertThat(selections.size(), is(2));
    assertThat(selections.get(0), notNullValue());
    assertThat(selections.get(0).getAlias(), nullValue());
    assertThat(selections.get(0).getSelectableNode(), instanceOf(SqmFunction.class));
    assertThat(((SqmFunction) selections.get(0).getSelectableNode()).getFunctionName(), is("count"));
    assertThat(selections.get(1), notNullValue());
    assertThat(selections.get(1).getAlias(), notNullValue());
    assertThat(selections.get(1).getAlias(), is("avg"));
    assertThat(selections.get(1).getSelectableNode(), instanceOf(SqmFunction.class));
    assertThat(((SqmFunction) selections.get(1).getSelectableNode()).getFunctionName(), is("avg"));
    s.delete(a);
    t.commit();
    s.close();
}
Also used : QuerySqmImpl(org.hibernate.query.sqm.internal.QuerySqmImpl) SqmSelectStatement(org.hibernate.query.sqm.tree.select.SqmSelectStatement) Transaction(org.hibernate.Transaction) SqmSelection(org.hibernate.query.sqm.tree.select.SqmSelection) SqmFunction(org.hibernate.query.sqm.tree.expression.SqmFunction) Session(org.hibernate.Session) Test(org.junit.Test)

Aggregations

SqmSelectStatement (org.hibernate.query.sqm.tree.select.SqmSelectStatement)17 SelectStatement (org.hibernate.sql.ast.tree.select.SelectStatement)9 ArrayList (java.util.ArrayList)4 SqmQueryImplementor (org.hibernate.query.hql.spi.SqmQueryImplementor)4 StandardSqmTranslator (org.hibernate.query.sqm.sql.internal.StandardSqmTranslator)4 TableGroup (org.hibernate.sql.ast.tree.from.TableGroup)4 HibernateException (org.hibernate.HibernateException)3 LoadQueryInfluencers (org.hibernate.engine.spi.LoadQueryInfluencers)3 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)3 EntityDomainType (org.hibernate.metamodel.model.domain.EntityDomainType)3 QueryEngine (org.hibernate.query.spi.QueryEngine)3 QueryOptions (org.hibernate.query.spi.QueryOptions)3 SqmQueryPart (org.hibernate.query.sqm.tree.select.SqmQueryPart)3 SqlSelection (org.hibernate.sql.ast.spi.SqlSelection)3 SelectClause (org.hibernate.sql.ast.tree.select.SelectClause)3 JdbcSelect (org.hibernate.sql.exec.spi.JdbcSelect)3 List (java.util.List)2 Map (java.util.Map)2 Session (org.hibernate.Session)2 HqlParser (org.hibernate.grammars.hql.HqlParser)2