Search in sources :

Example 1 with NamedNativeQueryDefinition

use of org.hibernate.boot.query.NamedNativeQueryDefinition in project hibernate-orm by hibernate.

the class QueryBinder method bindNativeQuery.

public static void bindNativeQuery(org.hibernate.annotations.NamedNativeQuery queryAnn, MetadataBuildingContext context) {
    if (queryAnn == null) {
        return;
    }
    final String registrationName = queryAnn.name();
    // ResultSetMappingDefinition mappingDefinition = mappings.getJdbcValuesMappingProducer( queryAnn.resultSetMapping() );
    if (BinderHelper.isEmptyAnnotationValue(registrationName)) {
        throw new AnnotationException("A named query must have a name when used in class or package level");
    }
    final String resultSetMappingName = queryAnn.resultSetMapping();
    final String resultSetMappingClassName = void.class.equals(queryAnn.resultClass()) ? null : queryAnn.resultClass().getName();
    final NamedNativeQueryDefinitionBuilder builder = new NamedNativeQueryDefinitionBuilder(registrationName).setSqlString(queryAnn.query()).setResultSetMappingName(resultSetMappingName).setResultSetMappingClassName(resultSetMappingClassName).setQuerySpaces(null).setCacheable(queryAnn.cacheable()).setCacheRegion(BinderHelper.getAnnotationValueStringOrNull(queryAnn.cacheRegion())).setCacheMode(getCacheMode(queryAnn.cacheMode())).setTimeout(queryAnn.timeout() < 0 ? null : queryAnn.timeout()).setFetchSize(queryAnn.fetchSize() < 0 ? null : queryAnn.fetchSize()).setFlushMode(getFlushMode(queryAnn.flushMode())).setReadOnly(queryAnn.readOnly()).setQuerySpaces(CollectionHelper.setOf(queryAnn.querySpaces())).setComment(BinderHelper.getAnnotationValueStringOrNull(queryAnn.comment()));
    if (queryAnn.callable()) {
        final NamedProcedureCallDefinition definition = createStoredProcedure(builder, context, () -> illegalCallSyntax(queryAnn, queryAnn.query()));
        context.getMetadataCollector().addNamedProcedureCallDefinition(definition);
        DeprecationLogger.DEPRECATION_LOGGER.warn("Marking named native queries as callable is no longer supported; use `@jakarta.persistence.NamedStoredProcedureQuery` instead.  Ignoring.");
    } else {
        final NamedNativeQueryDefinition queryDefinition = builder.build();
        if (LOG.isDebugEnabled()) {
            LOG.debugf("Binding named native query: %s => %s", queryDefinition.getRegistrationName(), queryDefinition.getSqlQueryString());
        }
        context.getMetadataCollector().addNamedNativeQuery(queryDefinition);
    }
}
Also used : NamedNativeQueryDefinitionBuilder(org.hibernate.boot.query.NamedNativeQueryDefinitionBuilder) AnnotationException(org.hibernate.AnnotationException) NamedNativeQueryDefinition(org.hibernate.boot.query.NamedNativeQueryDefinition) NamedProcedureCallDefinition(org.hibernate.boot.query.NamedProcedureCallDefinition)

Example 2 with NamedNativeQueryDefinition

use of org.hibernate.boot.query.NamedNativeQueryDefinition in project hibernate-orm by hibernate.

the class QueryBinder method bindNativeQuery.

public static void bindNativeQuery(NamedNativeQuery queryAnn, MetadataBuildingContext context, boolean isDefault) {
    if (queryAnn == null) {
        return;
    }
    if (BinderHelper.isEmptyAnnotationValue(queryAnn.name())) {
        throw new AnnotationException("A named query must have a name when used in class or package level");
    }
    final String registrationName = queryAnn.name();
    final String queryString = queryAnn.query();
    final QueryHintDefinition hints = new QueryHintDefinition(registrationName, queryAnn.hints());
    final String resultSetMappingName = queryAnn.resultSetMapping();
    final String resultSetMappingClassName = void.class.equals(queryAnn.resultClass()) ? null : queryAnn.resultClass().getName();
    final NamedNativeQueryDefinitionBuilder builder = new NamedNativeQueryDefinitionBuilder(registrationName).setSqlString(queryString).setResultSetMappingName(resultSetMappingName).setResultSetMappingClassName(resultSetMappingClassName).setQuerySpaces(null).setCacheable(hints.getCacheability()).setCacheMode(hints.getCacheMode()).setCacheRegion(hints.getString(HibernateHints.HINT_CACHE_REGION)).setTimeout(hints.getTimeout()).setFetchSize(hints.getInteger(HibernateHints.HINT_FETCH_SIZE)).setFlushMode(hints.getFlushMode()).setReadOnly(hints.getBoolean(HibernateHints.HINT_READ_ONLY)).setComment(hints.getString(HibernateHints.HINT_COMMENT)).addHints(hints.getHintsMap());
    final NamedNativeQueryDefinition queryDefinition = builder.build();
    if (LOG.isDebugEnabled()) {
        LOG.debugf("Binding named native query: %s => %s", queryDefinition.getRegistrationName(), queryDefinition.getSqlQueryString());
    }
    if (isDefault) {
        context.getMetadataCollector().addDefaultNamedNativeQuery(queryDefinition);
    } else {
        context.getMetadataCollector().addNamedNativeQuery(queryDefinition);
    }
}
Also used : NamedNativeQueryDefinitionBuilder(org.hibernate.boot.query.NamedNativeQueryDefinitionBuilder) AnnotationException(org.hibernate.AnnotationException) NamedNativeQueryDefinition(org.hibernate.boot.query.NamedNativeQueryDefinition)

Example 3 with NamedNativeQueryDefinition

use of org.hibernate.boot.query.NamedNativeQueryDefinition 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)

Aggregations

NamedNativeQueryDefinition (org.hibernate.boot.query.NamedNativeQueryDefinition)3 AnnotationException (org.hibernate.AnnotationException)2 NamedNativeQueryDefinitionBuilder (org.hibernate.boot.query.NamedNativeQueryDefinitionBuilder)2 NamedProcedureCallDefinition (org.hibernate.boot.query.NamedProcedureCallDefinition)2 NamedHqlQueryDefinition (org.hibernate.boot.query.NamedHqlQueryDefinition)1 NamedCallableQueryMemento (org.hibernate.procedure.spi.NamedCallableQueryMemento)1 NamedQueryMemento (org.hibernate.query.named.NamedQueryMemento)1 NamedNativeQueryMemento (org.hibernate.query.sql.spi.NamedNativeQueryMemento)1 NamedSqmQueryMemento (org.hibernate.query.sqm.spi.NamedSqmQueryMemento)1