Spring Данные JPA NamedStoredProcedureQuery Множественные параметры вывода
У меня есть простая хранимая процедура, которую я использую для тестирования Spring Функция JPA хранимой процедуры Data.
create or replace procedure plus1inout (arg in int,res1 out int,res2 out int) is
BEGIN
res1 := arg + 1;
res2 := res1 + 1;
END;
Мой код:
@Repository
public interface AdjudConverDateSPRepository extends JpaRepository<AdjudConverDateSP, Long> {
@Procedure(name = "plus1")
Object[] plus1(@Param("arg") Integer arg);
}
@Entity
@NamedStoredProcedureQuery(name = "plus1", procedureName = "ADJUD.PLUS1INOUT",
parameters = {
@StoredProcedureParameter(mode = ParameterMode.IN, name = "arg", type = Integer.class),
@StoredProcedureParameter(mode = ParameterMode.OUT, name = "res1", type = Integer.class),
@StoredProcedureParameter(mode = ParameterMode.OUT, name = "res2", type = Integer.class)
})
public class AdjudConverDateSP implements Serializable {
//stub to satisfy hibernate identifier requirement
@Id @GeneratedValue
private Long id;
}
Все работает отлично, когда у меня есть один параметр OUT. Но как только я добавляю второй параметр OUT, я получаю исключение, говоря, что он не может найти процедуру в сущности.
Caused by:
org.springframework.data.mapping.PropertyReferenceException: No property plus1 found for type AdjudConverDateSP! at
org.springframework.data.mapping.PropertyPath.<init>(PropertyPath.java:75) at
org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:327) at
org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:307) at
org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:270) at
org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:241) at
org.springframework.data.repository.query.parser.Part.<init>(Part.java:76) at
org.springframework.data.repository.query.parser.PartTree$OrPart.<init>(PartTree.java:235) at
org.springframework.data.repository.query.parser.PartTree$Predicate.buildTree(PartTree.java:373) at
org.springframework.data.repository.query.parser.PartTree$Predicate.<init>(PartTree.java:353)
Ответы
Ответ 1
Похоже, что @Procedure
ожидает только один параметр OUT, привязанный непосредственно к типу возвращаемого метода...
Для обработки нескольких параметров OUT вы можете напрямую использовать API JPA:
StoredProcedureQuery proc = em.createNamedStoredProcedureQuery("plus1");
proc.setParameter("arg", 1);
proc.execute();
Integer res1 = (Integer) proc.getOutputParameterValue("res1");
Integer res2 = (Integer) proc.getOutputParameterValue("res2");
...
Ответ 2
Вы можете указать, чтобы возвращать один из нескольких outputParameterName
параметров с параметром outputParameterName
в аннотации @Procedure
следующим образом:
@Repository
public interface AdjudConverDateSPRepository extends JpaRepository<AdjudConverDateSP, Long> {
@Procedure(name = "plus1", outputParameterName = "res2")
Integer plus1(@Param("arg") Integer arg);
}
ОБНОВЛЕНИЕ 2019-06-24:
В Spring Data JPA 2.2-RC1 теперь поддерживаются параметры множественного вывода https://spring.io/blog/2019/06/17/spring-data-moore-rc1-and-lovelace-sr9-released
https://jira.spring.io/browse/DATAJPA-707
Метод интерфейса просто должен иметь возвращаемый тип Map, чтобы к каждому выходному параметру можно было получить доступ по имени ключа:
@Repository
public interface AdjudConverDateSPRepository extends JpaRepository<AdjudConverDateSP, Long> {
@Procedure(name = "plus1")
Map<String, Object> plus1(@Param("arg") Integer arg);
}
Ответ 3
Spring пока не поддерживает несколько выходных параметров. Для этого есть JIRA.