Search in sources :

Example 1 with QueryInterpretationCache

use of org.hibernate.query.spi.QueryInterpretationCache 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 2 with QueryInterpretationCache

use of org.hibernate.query.spi.QueryInterpretationCache in project hibernate-orm by hibernate.

the class AbstractSharedSessionContract method createQuery.

@Override
public <T> QueryImplementor<T> createQuery(String queryString, Class<T> resultClass) {
    checkOpen();
    pulseTransactionCoordinator();
    delayedAfterCompletion();
    try {
        final QueryEngine queryEngine = getFactory().getQueryEngine();
        final QueryInterpretationCache interpretationCache = queryEngine.getInterpretationCache();
        final QuerySqmImpl<T> query = new QuerySqmImpl<>(queryString, interpretationCache.resolveHqlInterpretation(queryString, s -> queryEngine.getHqlTranslator().translate(queryString)), resultClass, this);
        applyQuerySettingsAndHints(query);
        query.setComment(queryString);
        return query;
    } catch (RuntimeException e) {
        markForRollbackOnly();
        throw getExceptionConverter().convert(e);
    }
}
Also used : QuerySqmImpl(org.hibernate.query.sqm.internal.QuerySqmImpl) NamedCallableQueryMemento(org.hibernate.procedure.spi.NamedCallableQueryMemento) TransactionImplementor(org.hibernate.engine.transaction.spi.TransactionImplementor) EntityPersister(org.hibernate.persister.entity.EntityPersister) MutationQuery(org.hibernate.query.MutationQuery) ExceptionConverter(org.hibernate.engine.spi.ExceptionConverter) NativeQueryTupleTransformer(org.hibernate.jpa.spi.NativeQueryTupleTransformer) NamedObjectRepository(org.hibernate.query.named.NamedObjectRepository) StandardRandomStrategy(org.hibernate.id.uuid.StandardRandomStrategy) HibernateCriteriaBuilder(org.hibernate.query.criteria.HibernateCriteriaBuilder) ObjectInputStream(java.io.ObjectInputStream) CriteriaDelete(jakarta.persistence.criteria.CriteriaDelete) Interceptor(org.hibernate.Interceptor) CriteriaUpdate(jakarta.persistence.criteria.CriteriaUpdate) UnknownNamedQueryException(org.hibernate.query.UnknownNamedQueryException) JtaTransactionCoordinatorImpl(org.hibernate.resource.transaction.backend.jta.internal.JtaTransactionCoordinatorImpl) SqmDmlStatement(org.hibernate.query.sqm.tree.SqmDmlStatement) NamedResultSetMappingMemento(org.hibernate.query.named.NamedResultSetMappingMemento) EntityNameResolver(org.hibernate.EntityNameResolver) Transaction(org.hibernate.Transaction) SqmStatement(org.hibernate.query.sqm.tree.SqmStatement) SqmQueryImplementor(org.hibernate.query.hql.spi.SqmQueryImplementor) SqmUtil(org.hibernate.query.sqm.internal.SqmUtil) Locale(java.util.Locale) SessionEventListenerManager(org.hibernate.engine.spi.SessionEventListenerManager) Query(org.hibernate.query.Query) SqmUpdateStatement(org.hibernate.query.sqm.tree.update.SqmUpdateStatement) HqlInterpretation(org.hibernate.query.spi.HqlInterpretation) FlushModeTypeHelper(org.hibernate.jpa.internal.util.FlushModeTypeHelper) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) SqmDeleteStatement(org.hibernate.query.sqm.tree.delete.SqmDeleteStatement) JdbcCoordinator(org.hibernate.engine.jdbc.spi.JdbcCoordinator) SqmSelectStatement(org.hibernate.query.sqm.tree.select.SqmSelectStatement) PhysicalConnectionHandlingMode(org.hibernate.resource.jdbc.spi.PhysicalConnectionHandlingMode) TimeZone(java.util.TimeZone) FlushModeType(jakarta.persistence.FlushModeType) StringHelper(org.hibernate.internal.util.StringHelper) UUID(java.util.UUID) Tuple(jakarta.persistence.Tuple) EmptyInterceptor(org.hibernate.EmptyInterceptor) WorkExecutorVisitable(org.hibernate.jdbc.WorkExecutorVisitable) QuerySqmImpl(org.hibernate.query.sqm.internal.QuerySqmImpl) SessionException(org.hibernate.SessionException) CacheMode(org.hibernate.CacheMode) List(java.util.List) NativeQueryImplementor(org.hibernate.query.sql.spi.NativeQueryImplementor) QueryTypeMismatchException(org.hibernate.query.QueryTypeMismatchException) HibernateException(org.hibernate.HibernateException) TransactionImpl(org.hibernate.engine.transaction.internal.TransactionImpl) NativeQueryImpl(org.hibernate.query.sql.internal.NativeQueryImpl) StatementInspector(org.hibernate.resource.jdbc.spi.StatementInspector) TRUE(java.lang.Boolean.TRUE) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor) JdbcConnectionAccess(org.hibernate.engine.jdbc.connections.spi.JdbcConnectionAccess) SessionEventListenerManagerImpl(org.hibernate.engine.internal.SessionEventListenerManagerImpl) SessionEventListener(org.hibernate.SessionEventListener) IllegalMutationQueryException(org.hibernate.query.IllegalMutationQueryException) TransactionCoordinator(org.hibernate.resource.transaction.spi.TransactionCoordinator) CacheTransactionSynchronization(org.hibernate.cache.spi.CacheTransactionSynchronization) Function(java.util.function.Function) UnknownEntityTypeException(org.hibernate.UnknownEntityTypeException) SQLException(java.sql.SQLException) SqmSelectionQuery(org.hibernate.query.sqm.SqmSelectionQuery) QueryImplementor(org.hibernate.query.spi.QueryImplementor) SqmQuerySpec(org.hibernate.query.sqm.tree.select.SqmQuerySpec) ObjectOutputStream(java.io.ObjectOutputStream) QueryInterpretationCache(org.hibernate.query.spi.QueryInterpretationCache) NamedSqmQueryMemento(org.hibernate.query.sqm.spi.NamedSqmQueryMemento) ProcedureCall(org.hibernate.procedure.ProcedureCall) LockMode(org.hibernate.LockMode) JdbcCoordinatorImpl(org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl) SqmSelectionQueryImpl(org.hibernate.query.sqm.internal.SqmSelectionQueryImpl) IllegalSelectQueryException(org.hibernate.query.IllegalSelectQueryException) IllegalNamedQueryOptionsException(org.hibernate.query.IllegalNamedQueryOptionsException) IOException(java.io.IOException) FlushMode(org.hibernate.FlushMode) JdbcServices(org.hibernate.engine.jdbc.spi.JdbcServices) EntityKey(org.hibernate.engine.spi.EntityKey) JdbcSessionContext(org.hibernate.resource.jdbc.spi.JdbcSessionContext) TransactionCoordinatorBuilder(org.hibernate.resource.transaction.spi.TransactionCoordinatorBuilder) Work(org.hibernate.jdbc.Work) SqmQueryGroup(org.hibernate.query.sqm.tree.select.SqmQueryGroup) QueryEngine(org.hibernate.query.spi.QueryEngine) CriteriaQuery(jakarta.persistence.criteria.CriteriaQuery) ProcedureCallImpl(org.hibernate.procedure.internal.ProcedureCallImpl) LobCreator(org.hibernate.engine.jdbc.LobCreator) NamedNativeQueryMemento(org.hibernate.query.sql.spi.NamedNativeQueryMemento) NamedNativeQuery(jakarta.persistence.NamedNativeQuery) TransactionRequiredException(jakarta.persistence.TransactionRequiredException) ReturningWork(org.hibernate.jdbc.ReturningWork) SelectionQuery(org.hibernate.query.SelectionQuery) QueryInterpretationCache(org.hibernate.query.spi.QueryInterpretationCache) QueryEngine(org.hibernate.query.spi.QueryEngine)

Example 3 with QueryInterpretationCache

use of org.hibernate.query.spi.QueryInterpretationCache 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 4 with QueryInterpretationCache

use of org.hibernate.query.spi.QueryInterpretationCache in project hibernate-orm by hibernate.

the class NativeQueryImpl method resolveParameterInterpretation.

private ParameterInterpretation resolveParameterInterpretation(String sqlString, SharedSessionContractImplementor session) {
    final SessionFactoryImplementor sessionFactory = session.getFactory();
    final QueryEngine queryEngine = sessionFactory.getQueryEngine();
    final QueryInterpretationCache interpretationCache = queryEngine.getInterpretationCache();
    return interpretationCache.resolveNativeQueryParameters(sqlString, s -> {
        final ParameterRecognizerImpl parameterRecognizer = new ParameterRecognizerImpl();
        session.getFactory().getServiceRegistry().getService(NativeQueryInterpreter.class).recognizeParameters(sqlString, parameterRecognizer);
        return new ParameterInterpretationImpl(parameterRecognizer);
    });
}
Also used : QueryInterpretationCache(org.hibernate.query.spi.QueryInterpretationCache) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) QueryEngine(org.hibernate.query.spi.QueryEngine) NativeQueryInterpreter(org.hibernate.engine.query.spi.NativeQueryInterpreter)

Aggregations

QueryInterpretationCache (org.hibernate.query.spi.QueryInterpretationCache)4 HibernateException (org.hibernate.HibernateException)2 QueryEngine (org.hibernate.query.spi.QueryEngine)2 NamedNativeQueryMemento (org.hibernate.query.sql.spi.NamedNativeQueryMemento)2 NamedSqmQueryMemento (org.hibernate.query.sqm.spi.NamedSqmQueryMemento)2 FlushModeType (jakarta.persistence.FlushModeType)1 NamedNativeQuery (jakarta.persistence.NamedNativeQuery)1 TransactionRequiredException (jakarta.persistence.TransactionRequiredException)1 Tuple (jakarta.persistence.Tuple)1 CriteriaDelete (jakarta.persistence.criteria.CriteriaDelete)1 CriteriaQuery (jakarta.persistence.criteria.CriteriaQuery)1 CriteriaUpdate (jakarta.persistence.criteria.CriteriaUpdate)1 IOException (java.io.IOException)1 ObjectInputStream (java.io.ObjectInputStream)1 ObjectOutputStream (java.io.ObjectOutputStream)1 TRUE (java.lang.Boolean.TRUE)1 SQLException (java.sql.SQLException)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Locale (java.util.Locale)1