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);
}
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;
}
}
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();
}
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();
}
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();
}
Aggregations