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));
}
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;
}
Aggregations