use of org.hibernate.orm.test.mapping.SmokeTests.Gender in project hibernate-orm by hibernate.
the class SmokeTests method testConvertedHqlInterpretation.
@Test
public void testConvertedHqlInterpretation(SessionFactoryScope scope) {
scope.inTransaction(session -> {
final JdbcTypeRegistry jdbcTypeRegistry = session.getFactory().getTypeConfiguration().getJdbcTypeRegistry();
final QueryImplementor<Gender> query = session.createQuery("select e.gender from SimpleEntity e", Gender.class);
final SqmQueryImplementor<Gender> hqlQuery = (SqmQueryImplementor<Gender>) query;
final SqmSelectStatement<Gender> sqmStatement = (SqmSelectStatement<Gender>) 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());
assertThat(sqlSelection, instanceOf(SqlSelectionImpl.class));
final Expression selectedExpression = sqlSelection.getExpression();
assertThat(selectedExpression, instanceOf(ColumnReference.class));
final ColumnReference columnReference = (ColumnReference) selectedExpression;
assertThat(columnReference.renderSqlFragment(scope.getSessionFactory()), is("s1_0.gender"));
final JdbcMappingContainer selectedExpressible = selectedExpression.getExpressionType();
assertThat(selectedExpressible, instanceOf(BasicTypeImpl.class));
final BasicTypeImpl<?> basicType = (BasicTypeImpl<?>) selectedExpressible;
assertThat(basicType.getJavaTypeDescriptor().getJavaTypeClass(), AssignableMatcher.assignableTo(Integer.class));
assertThat(basicType.getJdbcType(), is(jdbcTypeRegistry.getDescriptor(Types.TINYINT)));
assertThat(sqlAst.getDomainResultDescriptors().size(), is(1));
final DomainResult<?> domainResult = sqlAst.getDomainResultDescriptors().get(0);
assertThat(domainResult, instanceOf(BasicResult.class));
final BasicResult<?> scalarDomainResult = (BasicResult<?>) domainResult;
assertThat(scalarDomainResult.getAssembler(), instanceOf(BasicResultAssembler.class));
final BasicResultAssembler<?> assembler = (BasicResultAssembler<?>) scalarDomainResult.getAssembler();
assertThat(assembler.getValueConverter(), notNullValue());
assertThat(assembler.getValueConverter(), instanceOf(OrdinalEnumValueConverter.class));
final NavigablePath expectedSelectedPath = new NavigablePath(SimpleEntity.class.getName(), "e").append("gender");
assertThat(domainResult.getNavigablePath(), equalTo(expectedSelectedPath));
assertThat(domainResult, instanceOf(BasicResult.class));
// ScalarDomainResultImpl creates and caches the assembler at its creation.
// this just gets access to that cached one
final DomainResultAssembler<?> resultAssembler = domainResult.createResultAssembler(null, null);
assertThat(resultAssembler, instanceOf(BasicResultAssembler.class));
final BasicValueConverter<?, ?> valueConverter = ((BasicResultAssembler<?>) resultAssembler).getValueConverter();
assertThat(valueConverter, notNullValue());
assertThat(valueConverter, instanceOf(OrdinalEnumValueConverter.class));
final JdbcSelect jdbcSelectOperation = new StandardSqlAstTranslator<JdbcSelect>(session.getSessionFactory(), sqlAst).translate(null, QueryOptions.NONE);
assertThat(jdbcSelectOperation.getSql(), is("select s1_0.gender from mapping_simple_entity s1_0"));
});
}
Aggregations