Search in sources :

Example 1 with HqlTranslator

use of org.hibernate.query.hql.HqlTranslator in project hibernate-orm by hibernate.

the class QueryEngine method from.

public static QueryEngine from(SessionFactoryImplementor sessionFactory, MetadataImplementor metadata) {
    final QueryEngineOptions queryEngineOptions = sessionFactory.getSessionFactoryOptions();
    final SqmCreationOptions sqmCreationOptions = new SqmCreationOptionsStandard(sessionFactory);
    final Dialect dialect = sessionFactory.getJdbcServices().getDialect();
    final HqlTranslator hqlTranslator = resolveHqlTranslator(queryEngineOptions, dialect, sessionFactory, sqmCreationOptions);
    final SqmTranslatorFactory sqmTranslatorFactory = resolveSqmTranslatorFactory(queryEngineOptions, dialect);
    final SqmFunctionRegistry customSqmFunctionRegistry;
    if (queryEngineOptions.getCustomSqmFunctionRegistry() == null) {
        final Map<String, SqmFunctionDescriptor> customSqlFunctionMap = queryEngineOptions.getCustomSqlFunctionMap();
        if (customSqlFunctionMap == null || customSqlFunctionMap.isEmpty()) {
            customSqmFunctionRegistry = null;
        } else {
            customSqmFunctionRegistry = new SqmFunctionRegistry();
            customSqlFunctionMap.forEach(customSqmFunctionRegistry::register);
        }
    } else {
        customSqmFunctionRegistry = queryEngineOptions.getCustomSqmFunctionRegistry();
    }
    return new QueryEngine(sessionFactory.getUuid(), sessionFactory.getName(), sessionFactory.getSessionFactoryOptions().getJpaCompliance(), () -> sessionFactory.getRuntimeMetamodels().getJpaMetamodel(), sessionFactory.getSessionFactoryOptions().getCriteriaValueHandlingMode(), sessionFactory.getSessionFactoryOptions().getPreferredSqlTypeCodeForBoolean(), metadata.buildNamedQueryRepository(sessionFactory), hqlTranslator, sqmTranslatorFactory, sessionFactory.getServiceRegistry().getService(NativeQueryInterpreter.class), buildInterpretationCache(sessionFactory::getStatistics, sessionFactory.getProperties()), metadata.getTypeConfiguration(), dialect, customSqmFunctionRegistry, sessionFactory.getServiceRegistry());
}
Also used : SqmCreationOptionsStandard(org.hibernate.query.sqm.internal.SqmCreationOptionsStandard) SqmFunctionDescriptor(org.hibernate.query.sqm.function.SqmFunctionDescriptor) NativeQueryInterpreter(org.hibernate.engine.query.spi.NativeQueryInterpreter) SqmCreationOptions(org.hibernate.query.hql.spi.SqmCreationOptions) HqlTranslator(org.hibernate.query.hql.HqlTranslator) StandardHqlTranslator(org.hibernate.query.hql.internal.StandardHqlTranslator) Dialect(org.hibernate.dialect.Dialect) StandardSqmTranslatorFactory(org.hibernate.query.sqm.sql.StandardSqmTranslatorFactory) SqmTranslatorFactory(org.hibernate.query.sqm.sql.SqmTranslatorFactory) SqmFunctionRegistry(org.hibernate.query.sqm.function.SqmFunctionRegistry)

Example 2 with HqlTranslator

use of org.hibernate.query.hql.HqlTranslator in project hibernate-orm by hibernate.

the class NamedObjectRepositoryImpl method checkNamedQueries.

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Named query checking
public Map<String, HibernateException> checkNamedQueries(QueryEngine queryEngine) {
    Map<String, HibernateException> errors = new HashMap<>();
    final HqlTranslator sqmProducer = queryEngine.getHqlTranslator();
    final QueryInterpretationCache interpretationCache = queryEngine.getInterpretationCache();
    final boolean cachingEnabled = interpretationCache.isEnabled();
    // Check named HQL queries
    log.debugf("Checking %s named HQL queries", sqmMementoMap.size());
    for (NamedSqmQueryMemento hqlMemento : sqmMementoMap.values()) {
        try {
            log.debugf("Checking named HQL query: %s", hqlMemento.getRegistrationName());
            String queryString = hqlMemento.getHqlString();
            interpretationCache.resolveHqlInterpretation(queryString, s -> queryEngine.getHqlTranslator().translate(queryString));
        } catch (HibernateException e) {
            errors.put(hqlMemento.getRegistrationName(), e);
        }
    }
    // Check native-sql queries
    log.debugf("Checking %s named SQL queries", sqlMementoMap.size());
    for (NamedNativeQueryMemento memento : sqlMementoMap.values()) {
        memento.validate(queryEngine);
    // // this will throw an error if there's something wrong.
    // try {
    // log.debugf( "Checking named SQL query: %s", memento.getRegistrationName() );
    // // TODO : would be really nice to cache the spec on the query-def so as to not have to re-calc the hash;
    // // currently not doable though because of the resultset-ref stuff...
    // NativeSQLQuerySpecification spec;
    // if ( memento.getResultSetMappingName() != null ) {
    // NamedResultSetMappingMemento resultSetMappingMemento = getResultSetMappingMemento( memento.getResultSetMappingName() );
    // if ( resultSetMappingMemento == null ) {
    // throw new MappingException( "Unable to find resultset-ref resultSetMappingMemento: " + memento.getResultSetMappingName() );
    // }
    // spec = new NativeSQLQuerySpecification(
    // namedSQLQueryDefinition.getQueryString(),
    // resultSetMappingMemento.getQueryReturns(),
    // namedSQLQueryDefinition.getQuerySpaces()
    // );
    // }
    // else {
    // spec =  new NativeSQLQuerySpecification(
    // namedSQLQueryDefinition.getQueryString(),
    // namedSQLQueryDefinition.getQueryReturns(),
    // namedSQLQueryDefinition.getQuerySpaces()
    // );
    // }
    // queryEngine.getNativeSQLQueryPlan( spec );
    // }
    // catch ( HibernateException e ) {
    // errors.put( namedSQLQueryDefinition.getName(), e );
    // }
    }
    return errors;
}
Also used : HqlTranslator(org.hibernate.query.hql.HqlTranslator) NamedSqmQueryMemento(org.hibernate.query.sqm.spi.NamedSqmQueryMemento) HibernateException(org.hibernate.HibernateException) HashMap(java.util.HashMap) QueryInterpretationCache(org.hibernate.query.spi.QueryInterpretationCache) NamedNativeQueryMemento(org.hibernate.query.sql.spi.NamedNativeQueryMemento)

Example 3 with HqlTranslator

use of org.hibernate.query.hql.HqlTranslator 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);
        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)

Aggregations

HqlTranslator (org.hibernate.query.hql.HqlTranslator)3 SqmTranslatorFactory (org.hibernate.query.sqm.sql.SqmTranslatorFactory)2 HashMap (java.util.HashMap)1 HibernateException (org.hibernate.HibernateException)1 Dialect (org.hibernate.dialect.Dialect)1 NativeQueryInterpreter (org.hibernate.engine.query.spi.NativeQueryInterpreter)1 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)1 EntityMappingType (org.hibernate.metamodel.mapping.EntityMappingType)1 StandardHqlTranslator (org.hibernate.query.hql.internal.StandardHqlTranslator)1 SqmCreationOptions (org.hibernate.query.hql.spi.SqmCreationOptions)1 QueryEngine (org.hibernate.query.spi.QueryEngine)1 QueryInterpretationCache (org.hibernate.query.spi.QueryInterpretationCache)1 NamedNativeQueryMemento (org.hibernate.query.sql.spi.NamedNativeQueryMemento)1 SqmFunctionDescriptor (org.hibernate.query.sqm.function.SqmFunctionDescriptor)1 SqmFunctionRegistry (org.hibernate.query.sqm.function.SqmFunctionRegistry)1 SqmCreationOptionsStandard (org.hibernate.query.sqm.internal.SqmCreationOptionsStandard)1 NamedSqmQueryMemento (org.hibernate.query.sqm.spi.NamedSqmQueryMemento)1 StandardSqmTranslatorFactory (org.hibernate.query.sqm.sql.StandardSqmTranslatorFactory)1 SqmSelectStatement (org.hibernate.query.sqm.tree.select.SqmSelectStatement)1 TableGroup (org.hibernate.sql.ast.tree.from.TableGroup)1