Search in sources :

Example 1 with StoredProcedureParameter

use of jakarta.persistence.StoredProcedureParameter in project hibernate-orm by hibernate.

the class QueryBinder method createStoredProcedure.

public static NamedProcedureCallDefinition createStoredProcedure(NamedNativeQueryDefinitionBuilder builder, MetadataBuildingContext context, Supplier<RuntimeException> exceptionProducer) {
    List<StoredProcedureParameter> storedProcedureParameters = new ArrayList<>();
    List<QueryHint> queryHints = new ArrayList<>();
    List<String> parameterNames = new ArrayList<>();
    final String sqlString = builder.getSqlString().trim();
    if (!sqlString.startsWith("{") || !sqlString.endsWith("}")) {
        throw exceptionProducer.get();
    }
    final String procedureName = QueryBinder.parseJdbcCall(sqlString, parameterNames, exceptionProducer);
    AnnotationDescriptor ann = new AnnotationDescriptor(NamedStoredProcedureQuery.class);
    ann.setValue("name", builder.getName());
    ann.setValue("procedureName", procedureName);
    for (String parameterName : parameterNames) {
        AnnotationDescriptor parameterDescriptor = new AnnotationDescriptor(StoredProcedureParameter.class);
        parameterDescriptor.setValue("name", parameterName);
        parameterDescriptor.setValue("mode", ParameterMode.IN);
        final String typeName = builder.getParameterTypes().get(parameterName);
        if (typeName == null) {
            parameterDescriptor.setValue("type", Object.class);
        } else {
            final BasicType<Object> registeredType = context.getBootstrapContext().getTypeConfiguration().getBasicTypeRegistry().getRegisteredType(typeName);
            parameterDescriptor.setValue("type", registeredType.getJavaType());
        }
        storedProcedureParameters.add(AnnotationFactory.create(parameterDescriptor));
    }
    ann.setValue("parameters", storedProcedureParameters.toArray(new StoredProcedureParameter[storedProcedureParameters.size()]));
    if (builder.getResultSetMappingName() != null) {
        ann.setValue("resultSetMappings", new String[] { builder.getResultSetMappingName() });
    } else {
        ann.setValue("resultSetMappings", new String[0]);
    }
    if (builder.getResultSetMappingClassName() != null) {
        ann.setValue("resultClasses", new Class[] { context.getBootstrapContext().getClassLoaderAccess().classForName(builder.getResultSetMappingClassName()) });
    } else {
        ann.setValue("resultClasses", new Class[0]);
    }
    if (builder.getQuerySpaces() != null) {
        AnnotationDescriptor hintDescriptor = new AnnotationDescriptor(QueryHint.class);
        hintDescriptor.setValue("name", HibernateHints.HINT_NATIVE_SPACES);
        hintDescriptor.setValue("value", String.join(" ", builder.getQuerySpaces()));
        queryHints.add(AnnotationFactory.create(hintDescriptor));
    }
    AnnotationDescriptor hintDescriptor2 = new AnnotationDescriptor(QueryHint.class);
    hintDescriptor2.setValue("name", HibernateHints.HINT_CALLABLE_FUNCTION);
    hintDescriptor2.setValue("value", "true");
    queryHints.add(AnnotationFactory.create(hintDescriptor2));
    ann.setValue("hints", queryHints.toArray(new QueryHint[queryHints.size()]));
    return new NamedProcedureCallDefinitionImpl(AnnotationFactory.create(ann));
}
Also used : AnnotationDescriptor(org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor) NamedProcedureCallDefinitionImpl(org.hibernate.boot.internal.NamedProcedureCallDefinitionImpl) QueryHint(jakarta.persistence.QueryHint) ArrayList(java.util.ArrayList) StoredProcedureParameter(jakarta.persistence.StoredProcedureParameter)

Example 2 with StoredProcedureParameter

use of jakarta.persistence.StoredProcedureParameter in project hibernate-orm by hibernate.

the class JPAXMLOverriddenAnnotationReader method buildNamedStoreProcedureQueries.

public static List<NamedStoredProcedureQuery> buildNamedStoreProcedureQueries(List<JaxbNamedStoredProcedureQuery> elements, XMLContext.Default defaults, ClassLoaderAccess classLoaderAccess) {
    List<NamedStoredProcedureQuery> namedStoredProcedureQueries = new ArrayList<>();
    for (JaxbNamedStoredProcedureQuery element : elements) {
        AnnotationDescriptor ann = new AnnotationDescriptor(NamedStoredProcedureQuery.class);
        copyAttribute(ann, "name", element.getName(), true);
        copyAttribute(ann, "procedure-name", element.getProcedureName(), true);
        List<StoredProcedureParameter> storedProcedureParameters = new ArrayList<>();
        for (JaxbStoredProcedureParameter parameterElement : element.getParameter()) {
            AnnotationDescriptor parameterDescriptor = new AnnotationDescriptor(StoredProcedureParameter.class);
            copyAttribute(parameterDescriptor, "name", parameterElement.getName(), false);
            ParameterMode modeValue = parameterElement.getMode();
            if (modeValue == null) {
                parameterDescriptor.setValue("mode", ParameterMode.IN);
            } else {
                parameterDescriptor.setValue("mode", modeValue);
            }
            String clazzName = parameterElement.getClazz();
            Class<?> clazz;
            try {
                clazz = classLoaderAccess.classForName(XMLContext.buildSafeClassName(clazzName, defaults));
            } catch (ClassLoadingException e) {
                throw new AnnotationException("Unable to find entity-class: " + clazzName, e);
            }
            parameterDescriptor.setValue("type", clazz);
            storedProcedureParameters.add(AnnotationFactory.create(parameterDescriptor));
        }
        ann.setValue("parameters", storedProcedureParameters.toArray(new StoredProcedureParameter[storedProcedureParameters.size()]));
        List<Class<?>> returnClasses = new ArrayList<>();
        for (String clazzName : element.getResultClass()) {
            Class<?> clazz;
            try {
                clazz = classLoaderAccess.classForName(XMLContext.buildSafeClassName(clazzName, defaults));
            } catch (ClassLoadingException e) {
                throw new AnnotationException("Unable to find entity-class: " + clazzName, e);
            }
            returnClasses.add(clazz);
        }
        ann.setValue("resultClasses", returnClasses.toArray(new Class[returnClasses.size()]));
        ann.setValue("resultSetMappings", element.getResultSetMapping().toArray(new String[0]));
        buildQueryHints(element.getHint(), ann);
        namedStoredProcedureQueries.add(AnnotationFactory.create(ann));
    }
    return namedStoredProcedureQueries;
}
Also used : AnnotationDescriptor(org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor) ClassLoadingException(org.hibernate.boot.registry.classloading.spi.ClassLoadingException) ArrayList(java.util.ArrayList) JaxbStoredProcedureParameter(org.hibernate.boot.jaxb.mapping.spi.JaxbStoredProcedureParameter) JaxbStoredProcedureParameter(org.hibernate.boot.jaxb.mapping.spi.JaxbStoredProcedureParameter) StoredProcedureParameter(jakarta.persistence.StoredProcedureParameter) ParameterMode(jakarta.persistence.ParameterMode) AnnotationException(org.hibernate.AnnotationException) IdClass(jakarta.persistence.IdClass) MapKeyClass(jakarta.persistence.MapKeyClass) JaxbIdClass(org.hibernate.boot.jaxb.mapping.spi.JaxbIdClass) JaxbMapKeyClass(org.hibernate.boot.jaxb.mapping.spi.JaxbMapKeyClass) JaxbNamedStoredProcedureQuery(org.hibernate.boot.jaxb.mapping.spi.JaxbNamedStoredProcedureQuery) JaxbNamedStoredProcedureQuery(org.hibernate.boot.jaxb.mapping.spi.JaxbNamedStoredProcedureQuery) NamedStoredProcedureQuery(jakarta.persistence.NamedStoredProcedureQuery)

Aggregations

StoredProcedureParameter (jakarta.persistence.StoredProcedureParameter)2 ArrayList (java.util.ArrayList)2 AnnotationDescriptor (org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor)2 IdClass (jakarta.persistence.IdClass)1 MapKeyClass (jakarta.persistence.MapKeyClass)1 NamedStoredProcedureQuery (jakarta.persistence.NamedStoredProcedureQuery)1 ParameterMode (jakarta.persistence.ParameterMode)1 QueryHint (jakarta.persistence.QueryHint)1 AnnotationException (org.hibernate.AnnotationException)1 NamedProcedureCallDefinitionImpl (org.hibernate.boot.internal.NamedProcedureCallDefinitionImpl)1 JaxbIdClass (org.hibernate.boot.jaxb.mapping.spi.JaxbIdClass)1 JaxbMapKeyClass (org.hibernate.boot.jaxb.mapping.spi.JaxbMapKeyClass)1 JaxbNamedStoredProcedureQuery (org.hibernate.boot.jaxb.mapping.spi.JaxbNamedStoredProcedureQuery)1 JaxbStoredProcedureParameter (org.hibernate.boot.jaxb.mapping.spi.JaxbStoredProcedureParameter)1 ClassLoadingException (org.hibernate.boot.registry.classloading.spi.ClassLoadingException)1