Search in sources :

Example 6 with FromClause

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

the class SmokeTests method testSimpleHqlInterpretation.

@Test
public void testSimpleHqlInterpretation(SessionFactoryScope scope) {
    scope.inTransaction(session -> {
        final QueryImplementor<String> query = session.createQuery("select e.name from SimpleEntity e", String.class);
        final SqmQueryImplementor<String> hqlQuery = (SqmQueryImplementor<String>) query;
        final SqmSelectStatement<String> sqmStatement = (SqmSelectStatement<String>) hqlQuery.getSqmStatement();
        final StandardSqmTranslator<SelectStatement> sqmConverter = new StandardSqmTranslator<>(sqmStatement, hqlQuery.getQueryOptions(), ((QuerySqmImpl<?>) hqlQuery).getDomainParameterXref(), query.getParameterBindings(), session.getLoadQueryInfluencers(), scope.getSessionFactory(), true);
        final SqmTranslation<SelectStatement> sqmInterpretation = sqmConverter.translate();
        final SelectStatement sqlAst = sqmInterpretation.getSqlAst();
        final FromClause fromClause = sqlAst.getQuerySpec().getFromClause();
        assertThat(fromClause.getRoots().size(), is(1));
        final TableGroup rootTableGroup = fromClause.getRoots().get(0);
        assertThat(rootTableGroup.getPrimaryTableReference(), notNullValue());
        assertThat(rootTableGroup.getPrimaryTableReference().getTableId(), is("mapping_simple_entity"));
        assertThat(rootTableGroup.getTableReferenceJoins().size(), is(0));
        assertThat(rootTableGroup.getTableGroupJoins().isEmpty(), is(true));
        // `s` is the "alias stem" for `SimpleEntity` and as it is the first entity with that stem in
        // the query the base becomes `s1`.  The primary table reference is always suffixed as `_0`
        assertThat(rootTableGroup.getPrimaryTableReference().getIdentificationVariable(), is("s1_0"));
        final SelectClause selectClause = sqlAst.getQuerySpec().getSelectClause();
        assertThat(selectClause.getSqlSelections().size(), is(1));
        final SqlSelection sqlSelection = selectClause.getSqlSelections().get(0);
        assertThat(sqlSelection.getJdbcResultSetIndex(), is(1));
        assertThat(sqlSelection.getValuesArrayPosition(), is(0));
        assertThat(sqlSelection.getJdbcValueExtractor(), notNullValue());
        final JdbcSelect jdbcSelectOperation = new StandardSqlAstTranslator<JdbcSelect>(session.getSessionFactory(), sqlAst).translate(null, QueryOptions.NONE);
        assertThat(jdbcSelectOperation.getSql(), is("select s1_0.name from mapping_simple_entity s1_0"));
    });
}
Also used : SqmSelectStatement(org.hibernate.query.sqm.tree.select.SqmSelectStatement) SelectClause(org.hibernate.sql.ast.tree.select.SelectClause) JdbcSelect(org.hibernate.sql.exec.spi.JdbcSelect) TableGroup(org.hibernate.sql.ast.tree.from.TableGroup) SqlSelection(org.hibernate.sql.ast.spi.SqlSelection) SqmSelectStatement(org.hibernate.query.sqm.tree.select.SqmSelectStatement) SelectStatement(org.hibernate.sql.ast.tree.select.SelectStatement) SqmQueryImplementor(org.hibernate.query.hql.spi.SqmQueryImplementor) FromClause(org.hibernate.sql.ast.tree.from.FromClause) StandardSqmTranslator(org.hibernate.query.sqm.sql.internal.StandardSqmTranslator) Test(org.junit.jupiter.api.Test)

Example 7 with FromClause

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

the class CriteriaEntityGraphTest method assertEntityValuedJoinedGroup.

private void assertEntityValuedJoinedGroup(SelectStatement sqlAst, String expectedAttributeName, Class<?> expectedEntityJpaClass, Consumer<TableGroup> tableGroupConsumer) {
    final FromClause fromClause = sqlAst.getQuerySpec().getFromClause();
    assertThat(fromClause.getRoots(), hasSize(1));
    final TableGroup rootTableGroup = fromClause.getRoots().get(0);
    assertThat(rootTableGroup.getTableGroupJoins(), hasSize(1));
    final TableGroup joinedGroup = rootTableGroup.getTableGroupJoins().iterator().next().getJoinedGroup();
    assertThat(joinedGroup.getModelPart().getPartName(), is(expectedAttributeName));
    assertThat(joinedGroup.getModelPart().getJavaType().getJavaTypeClass(), assignableTo(expectedEntityJpaClass));
    assertThat(joinedGroup.getModelPart(), instanceOf(EntityValuedModelPart.class));
    tableGroupConsumer.accept(joinedGroup);
}
Also used : LazyTableGroup(org.hibernate.sql.ast.tree.from.LazyTableGroup) StandardVirtualTableGroup(org.hibernate.sql.ast.tree.from.StandardVirtualTableGroup) TableGroup(org.hibernate.sql.ast.tree.from.TableGroup) EntityValuedModelPart(org.hibernate.metamodel.mapping.EntityValuedModelPart) FromClause(org.hibernate.sql.ast.tree.from.FromClause)

Example 8 with FromClause

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

the class CriteriaEntityGraphTest method assertPluralAttributeJoinedGroup.

private void assertPluralAttributeJoinedGroup(SelectStatement sqlAst, String expectedPluralAttributeName, Consumer<TableGroup> tableGroupConsumer) {
    final FromClause fromClause = sqlAst.getQuerySpec().getFromClause();
    assertThat(fromClause.getRoots(), hasSize(1));
    final TableGroup root = fromClause.getRoots().get(0);
    assertThat(root.getTableGroupJoins(), hasSize(1));
    final TableGroup joinedGroup = root.getTableGroupJoins().iterator().next().getJoinedGroup();
    assertThat(joinedGroup.getModelPart().getPartName(), is(expectedPluralAttributeName));
    assertThat(joinedGroup.getModelPart(), instanceOf(PluralAttributeMapping.class));
    tableGroupConsumer.accept(joinedGroup);
}
Also used : LazyTableGroup(org.hibernate.sql.ast.tree.from.LazyTableGroup) StandardVirtualTableGroup(org.hibernate.sql.ast.tree.from.StandardVirtualTableGroup) TableGroup(org.hibernate.sql.ast.tree.from.TableGroup) FromClause(org.hibernate.sql.ast.tree.from.FromClause) PluralAttributeMapping(org.hibernate.metamodel.mapping.PluralAttributeMapping)

Example 9 with FromClause

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

the class HqlEntityGraphTest method assertEmptyJoinedGroup.

// util methods for verifying 'from-clause' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
private void assertEmptyJoinedGroup(SelectStatement sqlAst) {
    final FromClause fromClause = sqlAst.getQuerySpec().getFromClause();
    assertThat(fromClause.getRoots(), hasSize(1));
    final TableGroup rootTableGroup = fromClause.getRoots().get(0);
    assertThat(rootTableGroup.getTableGroupJoins(), isEmpty());
}
Also used : LazyTableGroup(org.hibernate.sql.ast.tree.from.LazyTableGroup) StandardVirtualTableGroup(org.hibernate.sql.ast.tree.from.StandardVirtualTableGroup) TableGroup(org.hibernate.sql.ast.tree.from.TableGroup) FromClause(org.hibernate.sql.ast.tree.from.FromClause)

Example 10 with FromClause

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

the class EntityGraphLoadPlanBuilderTest method assertEmptyJoinedGroup.

// util methods for verifying 'from-clause' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
private void assertEmptyJoinedGroup(SelectStatement sqlAst) {
    final FromClause fromClause = sqlAst.getQuerySpec().getFromClause();
    assertThat(fromClause.getRoots(), hasSize(1));
    final TableGroup rootTableGroup = fromClause.getRoots().get(0);
    assertThat(rootTableGroup.getTableGroupJoins(), isEmpty());
}
Also used : LazyTableGroup(org.hibernate.sql.ast.tree.from.LazyTableGroup) StandardVirtualTableGroup(org.hibernate.sql.ast.tree.from.StandardVirtualTableGroup) TableGroup(org.hibernate.sql.ast.tree.from.TableGroup) FromClause(org.hibernate.sql.ast.tree.from.FromClause)

Aggregations

FromClause (org.hibernate.sql.ast.tree.from.FromClause)11 TableGroup (org.hibernate.sql.ast.tree.from.TableGroup)11 LazyTableGroup (org.hibernate.sql.ast.tree.from.LazyTableGroup)9 StandardVirtualTableGroup (org.hibernate.sql.ast.tree.from.StandardVirtualTableGroup)9 EntityValuedModelPart (org.hibernate.metamodel.mapping.EntityValuedModelPart)3 PluralAttributeMapping (org.hibernate.metamodel.mapping.PluralAttributeMapping)3 SqmQueryImplementor (org.hibernate.query.hql.spi.SqmQueryImplementor)2 StandardSqmTranslator (org.hibernate.query.sqm.sql.internal.StandardSqmTranslator)2 SqmSelectStatement (org.hibernate.query.sqm.tree.select.SqmSelectStatement)2 SqlSelection (org.hibernate.sql.ast.spi.SqlSelection)2 SelectClause (org.hibernate.sql.ast.tree.select.SelectClause)2 SelectStatement (org.hibernate.sql.ast.tree.select.SelectStatement)2 JdbcSelect (org.hibernate.sql.exec.spi.JdbcSelect)2 Test (org.junit.jupiter.api.Test)2 JdbcMappingContainer (org.hibernate.metamodel.mapping.JdbcMappingContainer)1 OrdinalEnumValueConverter (org.hibernate.metamodel.model.convert.internal.OrdinalEnumValueConverter)1 Gender (org.hibernate.orm.test.mapping.SmokeTests.Gender)1 NavigablePath (org.hibernate.query.spi.NavigablePath)1 ColumnReference (org.hibernate.sql.ast.tree.expression.ColumnReference)1 Expression (org.hibernate.sql.ast.tree.expression.Expression)1