Search in sources :

Example 1 with NamedProcedureCallDefinitionImpl

use of org.hibernate.boot.internal.NamedProcedureCallDefinitionImpl in project hibernate-orm by hibernate.

the class QueryBinder method bindNamedStoredProcedureQuery.

public static void bindNamedStoredProcedureQuery(NamedStoredProcedureQuery annotation, MetadataBuildingContext context, boolean isDefault) {
    if (annotation == null) {
        return;
    }
    final String registrationName = annotation.name();
    if (BinderHelper.isEmptyAnnotationValue(registrationName)) {
        throw new AnnotationException("A named query must have a name when used in class or package level");
    }
    final NamedProcedureCallDefinitionImpl def = new NamedProcedureCallDefinitionImpl(annotation);
    if (isDefault) {
        context.getMetadataCollector().addDefaultNamedProcedureCall(def);
    } else {
        context.getMetadataCollector().addNamedProcedureCallDefinition(def);
    }
    LOG.debugf("Bound named stored procedure query : %s => %s", def.getRegistrationName(), def.getProcedureName());
}
Also used : NamedProcedureCallDefinitionImpl(org.hibernate.boot.internal.NamedProcedureCallDefinitionImpl) AnnotationException(org.hibernate.AnnotationException)

Example 2 with NamedProcedureCallDefinitionImpl

use of org.hibernate.boot.internal.NamedProcedureCallDefinitionImpl 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)

Aggregations

NamedProcedureCallDefinitionImpl (org.hibernate.boot.internal.NamedProcedureCallDefinitionImpl)2 QueryHint (jakarta.persistence.QueryHint)1 StoredProcedureParameter (jakarta.persistence.StoredProcedureParameter)1 ArrayList (java.util.ArrayList)1 AnnotationException (org.hibernate.AnnotationException)1 AnnotationDescriptor (org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor)1