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