Search in sources :

Example 1 with NamedNativeQueryMemento

use of org.hibernate.query.sql.spi.NamedNativeQueryMemento 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 NamedNativeQueryMemento

use of org.hibernate.query.sql.spi.NamedNativeQueryMemento 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 NamedNativeQueryMemento

use of org.hibernate.query.sql.spi.NamedNativeQueryMemento 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 NamedNativeQueryMemento

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

the class NamedObjectRepositoryImpl method resolve.

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Prepare repository for use
@Override
public NamedQueryMemento resolve(SessionFactoryImplementor sessionFactory, MetadataImplementor bootMetamodel, String registrationName) {
    NamedQueryMemento namedQuery = sqlMementoMap.get(registrationName);
    if (namedQuery != null) {
        return namedQuery;
    }
    namedQuery = sqmMementoMap.get(registrationName);
    if (namedQuery != null) {
        return namedQuery;
    }
    namedQuery = callableMementoMap.get(registrationName);
    if (namedQuery != null) {
        return namedQuery;
    }
    final NamedHqlQueryDefinition namedHqlQueryDefinition = bootMetamodel.getNamedHqlQueryMapping(registrationName);
    if (namedHqlQueryDefinition != null) {
        final NamedSqmQueryMemento resolved = namedHqlQueryDefinition.resolve(sessionFactory);
        sqmMementoMap.put(namedHqlQueryDefinition.getRegistrationName(), resolved);
        return resolved;
    }
    final NamedNativeQueryDefinition namedNativeQueryDefinition = bootMetamodel.getNamedNativeQueryMapping(registrationName);
    if (namedNativeQueryDefinition != null) {
        final NamedNativeQueryMemento resolved = namedNativeQueryDefinition.resolve(sessionFactory);
        sqlMementoMap.put(namedNativeQueryDefinition.getRegistrationName(), resolved);
        return resolved;
    }
    final NamedProcedureCallDefinition namedCallableQueryDefinition = bootMetamodel.getNamedProcedureCallMapping(registrationName);
    if (namedCallableQueryDefinition != null) {
        final NamedCallableQueryMemento resolved = namedCallableQueryDefinition.resolve(sessionFactory);
        callableMementoMap.put(namedCallableQueryDefinition.getRegistrationName(), resolved);
        return resolved;
    }
    return null;
}
Also used : NamedSqmQueryMemento(org.hibernate.query.sqm.spi.NamedSqmQueryMemento) NamedHqlQueryDefinition(org.hibernate.boot.query.NamedHqlQueryDefinition) NamedNativeQueryMemento(org.hibernate.query.sql.spi.NamedNativeQueryMemento) NamedCallableQueryMemento(org.hibernate.procedure.spi.NamedCallableQueryMemento) NamedNativeQueryDefinition(org.hibernate.boot.query.NamedNativeQueryDefinition) NamedQueryMemento(org.hibernate.query.named.NamedQueryMemento) NamedProcedureCallDefinition(org.hibernate.boot.query.NamedProcedureCallDefinition)

Example 5 with NamedNativeQueryMemento

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

the class AbstractSharedSessionContract method buildNamedQuery.

@SuppressWarnings("rawtypes")
protected QueryImplementor buildNamedQuery(String queryName, Function<NamedSqmQueryMemento, SqmQueryImplementor> namedSqmHandler, Function<NamedNativeQueryMemento, NativeQueryImplementor> namedNativeHandler) {
    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 NamedObjectRepository namedObjectRepository = getFactory().getQueryEngine().getNamedObjectRepository();
    final NamedSqmQueryMemento namedSqmQueryMemento = namedObjectRepository.getSqmQueryMemento(queryName);
    if (namedSqmQueryMemento != null) {
        return namedSqmHandler.apply(namedSqmQueryMemento);
    }
    // otherwise, see if it is a named native query
    final NamedNativeQueryMemento namedNativeDescriptor = namedObjectRepository.getNativeQueryMemento(queryName);
    if (namedNativeDescriptor != null) {
        return namedNativeHandler.apply(namedNativeDescriptor);
    }
    throw new UnknownNamedQueryException(queryName);
}
Also used : NamedSqmQueryMemento(org.hibernate.query.sqm.spi.NamedSqmQueryMemento) NamedObjectRepository(org.hibernate.query.named.NamedObjectRepository) NamedNativeQueryMemento(org.hibernate.query.sql.spi.NamedNativeQueryMemento) UnknownNamedQueryException(org.hibernate.query.UnknownNamedQueryException)

Aggregations

NamedNativeQueryMemento (org.hibernate.query.sql.spi.NamedNativeQueryMemento)5 NamedSqmQueryMemento (org.hibernate.query.sqm.spi.NamedSqmQueryMemento)5 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 NamedObjectRepository (org.hibernate.query.named.NamedObjectRepository)1 NamedQueryMemento (org.hibernate.query.named.NamedQueryMemento)1 NamedResultSetMappingMemento (org.hibernate.query.named.NamedResultSetMappingMemento)1 QueryInterpretationCache (org.hibernate.query.spi.QueryInterpretationCache)1