Search in sources :

Example 1 with NamedSqmQueryMemento

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

the class NamedObjectRepositoryImpl method prepare.

@Override
public void prepare(SessionFactoryImplementor sessionFactory, MetadataImplementor bootMetamodel, BootstrapContext bootstrapContext) {
    bootMetamodel.visitNamedHqlQueryDefinitions(namedHqlQueryDefinition -> {
        final NamedSqmQueryMemento resolved = namedHqlQueryDefinition.resolve(sessionFactory);
        sqmMementoMap.put(namedHqlQueryDefinition.getRegistrationName(), resolved);
    });
    bootMetamodel.visitNamedNativeQueryDefinitions(namedNativeQueryDefinition -> {
        final NamedNativeQueryMemento resolved = namedNativeQueryDefinition.resolve(sessionFactory);
        sqlMementoMap.put(namedNativeQueryDefinition.getRegistrationName(), resolved);
    });
    bootMetamodel.visitNamedResultSetMappingDefinition(namedResultSetMappingDefinition -> {
        final NamedResultSetMappingMemento resolved = namedResultSetMappingDefinition.resolve(() -> sessionFactory);
        resultSetMappingMementoMap.put(namedResultSetMappingDefinition.getRegistrationName(), resolved);
    });
    bootMetamodel.visitNamedProcedureCallDefinition(namedProcedureCallDefinition -> {
        final NamedCallableQueryMemento resolved = namedProcedureCallDefinition.resolve(sessionFactory);
        callableMementoMap.put(namedProcedureCallDefinition.getRegistrationName(), resolved);
    });
}
Also used : NamedSqmQueryMemento(org.hibernate.query.sqm.spi.NamedSqmQueryMemento) NamedNativeQueryMemento(org.hibernate.query.sql.spi.NamedNativeQueryMemento) NamedResultSetMappingMemento(org.hibernate.query.named.NamedResultSetMappingMemento) NamedCallableQueryMemento(org.hibernate.procedure.spi.NamedCallableQueryMemento)

Example 2 with NamedSqmQueryMemento

use of org.hibernate.query.sqm.spi.NamedSqmQueryMemento 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 NamedSqmQueryMemento

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

the class AbstractSharedSessionContract method createNamedSelectionQuery.

@Override
public <R> SelectionQuery<R> createNamedSelectionQuery(String queryName, Class<R> expectedResultType) {
    checkOpen();
    pulseTransactionCoordinator();
    delayedAfterCompletion();
    // this method can be called for either a named HQL query or a named native query
    // first see if it is a named HQL query
    final NamedSqmQueryMemento namedHqlDescriptor = getFactory().getQueryEngine().getNamedObjectRepository().getSqmQueryMemento(queryName);
    if (namedHqlDescriptor != null) {
        return createNamedSqmSelectionQuery(namedHqlDescriptor, expectedResultType);
    }
    // otherwise, see if it is a named native query
    final NamedNativeQueryMemento namedNativeDescriptor = getFactory().getQueryEngine().getNamedObjectRepository().getNativeQueryMemento(queryName);
    if (namedNativeDescriptor != null) {
        return createNamedNativeSelectionQuery(namedNativeDescriptor, expectedResultType);
    }
    throw new UnknownNamedQueryException(queryName);
}
Also used : NamedSqmQueryMemento(org.hibernate.query.sqm.spi.NamedSqmQueryMemento) NamedNativeQueryMemento(org.hibernate.query.sql.spi.NamedNativeQueryMemento) UnknownNamedQueryException(org.hibernate.query.UnknownNamedQueryException)

Example 4 with NamedSqmQueryMemento

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

the class SimpleNamedQueryTests method testBinding.

@Test
public void testBinding(SessionFactoryScope scope) {
    final NamedObjectRepository namedObjectRepository = scope.getSessionFactory().getQueryEngine().getNamedObjectRepository();
    final NamedSqmQueryMemento simpleMemento = namedObjectRepository.getSqmQueryMemento("simple");
    assertThat(simpleMemento, notNullValue());
    final NamedSqmQueryMemento restrictedMemento = namedObjectRepository.getSqmQueryMemento("restricted");
    assertThat(restrictedMemento, notNullValue());
}
Also used : NamedSqmQueryMemento(org.hibernate.query.sqm.spi.NamedSqmQueryMemento) NamedObjectRepository(org.hibernate.query.named.NamedObjectRepository) Test(org.junit.jupiter.api.Test)

Example 5 with NamedSqmQueryMemento

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

the class SimpleNamedQueryTests method testExecution.

@Test
public void testExecution(SessionFactoryScope scope) {
    scope.inTransaction(session -> {
        session.createNamedQuery("simple").list();
        session.createNamedQuery("restricted").setParameter("name", "a name").list();
    });
    final NamedObjectRepository namedObjectRepository = scope.getSessionFactory().getQueryEngine().getNamedObjectRepository();
    final NamedSqmQueryMemento simpleMemento = namedObjectRepository.getSqmQueryMemento("simple");
    assertThat(simpleMemento, notNullValue());
    final NamedSqmQueryMemento restrictedMemento = namedObjectRepository.getSqmQueryMemento("restricted");
    assertThat(restrictedMemento, notNullValue());
}
Also used : NamedSqmQueryMemento(org.hibernate.query.sqm.spi.NamedSqmQueryMemento) NamedObjectRepository(org.hibernate.query.named.NamedObjectRepository) Test(org.junit.jupiter.api.Test)

Aggregations

NamedSqmQueryMemento (org.hibernate.query.sqm.spi.NamedSqmQueryMemento)9 NamedNativeQueryMemento (org.hibernate.query.sql.spi.NamedNativeQueryMemento)5 Test (org.junit.jupiter.api.Test)4 NamedObjectRepository (org.hibernate.query.named.NamedObjectRepository)3 Query (jakarta.persistence.Query)2 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)2 NamedCallableQueryMemento (org.hibernate.procedure.spi.NamedCallableQueryMemento)2 UnknownNamedQueryException (org.hibernate.query.UnknownNamedQueryException)2 HashMap (java.util.HashMap)1 HibernateException (org.hibernate.HibernateException)1 NamedHqlQueryDefinition (org.hibernate.boot.query.NamedHqlQueryDefinition)1 NamedNativeQueryDefinition (org.hibernate.boot.query.NamedNativeQueryDefinition)1 NamedProcedureCallDefinition (org.hibernate.boot.query.NamedProcedureCallDefinition)1 HqlTranslator (org.hibernate.query.hql.HqlTranslator)1 NamedQueryMemento (org.hibernate.query.named.NamedQueryMemento)1 NamedResultSetMappingMemento (org.hibernate.query.named.NamedResultSetMappingMemento)1 QueryInterpretationCache (org.hibernate.query.spi.QueryInterpretationCache)1