Search in sources :

Example 1 with NamedProcedureCallDefinition

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

the class NamedQueryBinder method processNamedNativeQuery.

public static void processNamedNativeQuery(final HbmLocalMetadataBuildingContext context, JaxbHbmNamedNativeQueryType namedQueryBinding, String prefix) {
    if (namedQueryBinding.isCallable()) {
        DeprecationLogger.DEPRECATION_LOGGER.warn("Marking named native queries as callable is no longer supported; use `@jakarta.persistence.NamedStoredProcedureQuery` instead.  Ignoring.");
    }
    final String registrationName = prefix + namedQueryBinding.getName();
    final NamedNativeQueryDefinitionBuilder builder = new NamedNativeQueryDefinitionBuilder(registrationName).setComment(namedQueryBinding.getComment()).setCacheable(namedQueryBinding.isCacheable()).setCacheMode(namedQueryBinding.getCacheMode()).setCacheRegion(namedQueryBinding.getCacheRegion()).setTimeout(namedQueryBinding.getTimeout()).setReadOnly(namedQueryBinding.isReadOnly()).setFlushMode(namedQueryBinding.getFlushMode()).setFetchSize(namedQueryBinding.getFetchSize()).setResultSetMappingName(namedQueryBinding.getResultsetRef());
    final ImplicitHbmResultSetMappingDescriptorBuilder implicitResultSetMappingBuilder = new ImplicitHbmResultSetMappingDescriptorBuilder(registrationName, context);
    boolean foundQuery = false;
    for (Object content : namedQueryBinding.getContent()) {
        final boolean wasQuery = processNamedQueryContentItem(content, builder, implicitResultSetMappingBuilder, namedQueryBinding, context);
        if (wasQuery) {
            foundQuery = true;
        }
    }
    if (!foundQuery) {
        throw new org.hibernate.boot.MappingException(String.format("Named native query [%s] did not specify query string", namedQueryBinding.getName()), context.getOrigin());
    }
    if (implicitResultSetMappingBuilder.hasAnyReturns()) {
        if (StringHelper.isNotEmpty(namedQueryBinding.getResultsetRef())) {
            throw new org.hibernate.boot.MappingException(String.format("Named native query [%s] specified both a resultset-ref and an inline mapping of results", namedQueryBinding.getName()), context.getOrigin());
        }
        context.getMetadataCollector().addResultSetMapping(implicitResultSetMappingBuilder.build(context));
        builder.setResultSetMappingName(implicitResultSetMappingBuilder.getRegistrationName());
    }
    if (namedQueryBinding.isCallable()) {
        final NamedProcedureCallDefinition definition = QueryBinder.createStoredProcedure(builder, context, () -> illegalCallSyntax(context, namedQueryBinding, builder.getSqlString()));
        context.getMetadataCollector().addNamedProcedureCallDefinition(definition);
        DeprecationLogger.DEPRECATION_LOGGER.warn("Marking named native queries as callable is deprecated; use `<named-stored-procedure-query/>` instead.");
    } else {
        context.getMetadataCollector().addNamedNativeQuery(builder.build());
    }
}
Also used : ImplicitHbmResultSetMappingDescriptorBuilder(org.hibernate.boot.query.ImplicitHbmResultSetMappingDescriptorBuilder) NamedNativeQueryDefinitionBuilder(org.hibernate.boot.query.NamedNativeQueryDefinitionBuilder) NamedProcedureCallDefinition(org.hibernate.boot.query.NamedProcedureCallDefinition) MappingException(org.hibernate.boot.MappingException)

Example 2 with NamedProcedureCallDefinition

use of org.hibernate.boot.query.NamedProcedureCallDefinition 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 3 with NamedProcedureCallDefinition

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

the class InFlightMetadataCollectorImpl method addNamedProcedureCallDefinition.

@Override
public void addNamedProcedureCallDefinition(NamedProcedureCallDefinition definition) {
    if (definition == null) {
        throw new IllegalArgumentException("Named query definition is null");
    }
    final String name = definition.getRegistrationName();
    if (defaultNamedProcedureNames.contains(name)) {
        return;
    }
    final NamedProcedureCallDefinition previous = namedProcedureCallMap.put(name, definition);
    if (previous != null) {
        throw new DuplicateMappingException(DuplicateMappingException.Type.PROCEDURE, name);
    }
}
Also used : DuplicateMappingException(org.hibernate.DuplicateMappingException) NamedProcedureCallDefinition(org.hibernate.boot.query.NamedProcedureCallDefinition)

Example 4 with NamedProcedureCallDefinition

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

NamedProcedureCallDefinition (org.hibernate.boot.query.NamedProcedureCallDefinition)4 NamedNativeQueryDefinition (org.hibernate.boot.query.NamedNativeQueryDefinition)2 NamedNativeQueryDefinitionBuilder (org.hibernate.boot.query.NamedNativeQueryDefinitionBuilder)2 AnnotationException (org.hibernate.AnnotationException)1 DuplicateMappingException (org.hibernate.DuplicateMappingException)1 MappingException (org.hibernate.boot.MappingException)1 ImplicitHbmResultSetMappingDescriptorBuilder (org.hibernate.boot.query.ImplicitHbmResultSetMappingDescriptorBuilder)1 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