Search in sources :

Example 1 with QueryHint

use of jakarta.persistence.QueryHint 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 QueryHint

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

the class JPAXMLOverriddenAnnotationReader method buildQueryHints.

private static void buildQueryHints(List<JaxbQueryHint> elements, AnnotationDescriptor ann) {
    List<QueryHint> queryHints = new ArrayList<>(elements.size());
    for (JaxbQueryHint hint : elements) {
        AnnotationDescriptor hintDescriptor = new AnnotationDescriptor(QueryHint.class);
        String value = hint.getName();
        if (value == null) {
            throw new AnnotationException("<hint> without name. " + SCHEMA_VALIDATION);
        }
        hintDescriptor.setValue("name", value);
        value = hint.getValue();
        if (value == null) {
            throw new AnnotationException("<hint> without value. " + SCHEMA_VALIDATION);
        }
        hintDescriptor.setValue("value", value);
        queryHints.add(AnnotationFactory.create(hintDescriptor));
    }
    ann.setValue("hints", queryHints.toArray(new QueryHint[queryHints.size()]));
}
Also used : AnnotationDescriptor(org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor) JaxbQueryHint(org.hibernate.boot.jaxb.mapping.spi.JaxbQueryHint) QueryHint(jakarta.persistence.QueryHint) JaxbQueryHint(org.hibernate.boot.jaxb.mapping.spi.JaxbQueryHint) ArrayList(java.util.ArrayList) AnnotationException(org.hibernate.AnnotationException)

Aggregations

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