[英]How to call a stored procedure in Spring Boot?
A query for a stored procedure for multiple delete yield an error message shown way below.查询多个删除的存储过程会产生如下所示的错误消息。
CREATE OR REPLACE PROCEDURE delete_dim_page(_page_id bigint)
LANGUAGE plpgsql
AS $procedure$
begin
delete from page_catalog where page_type_id = _page_id;
delete from page_type where page_type_id = _page_id;
END;
$procedure$
;
//entityclass //实体类
@NamedStoredProcedureQuery(
name = "firstProcedure",
procedureName = "delete_dim_page",
parameters = {
@StoredProcedureParameter(
mode=ParameterMode.IN,
name="_page_id",
type = Long.class
)
}
)
//service.java //服务.java
StoredProcedureQuery query = em.createNamedStoredProcedureQuery("firstProcedure");
query.setParameter("_page_id", pageTypeId);
query.execute();
yields产量
ERROR: delete_dim_page1(bigint) is a procedure
错误:delete_dim_page1(bigint) 是一个过程
Hint: To call a procedure, use CALL.提示:要调用过程,请使用 CALL。
What is a recommendable way to call a stored procedure in Spring Boot?在 Spring Boot 中调用存储过程的推荐方式是什么?
I suppose yiu are using spring data jpa.我想你正在使用 spring 数据 jpa。 You can just create a jpa repository et call your procedure with a annontation like that:
您可以创建一个 jpa 存储库并使用如下注释调用您的过程:
public interface PageCatalogRepository extends JpaRepository<PageCatalogEntity , Long> {
@Query(value = "CALL firstProcedure(:page_id);", nativeQuery = true)
void deletePageCatalogById(@Param("page_id") Long page_id);
}
You can find other procedure calling method here https://www.baeldung.com/spring-data-jpa-stored-procedures您可以在此处找到其他程序调用方法https://www.baeldung.com/spring-data-jpa-stored-procedures
When using Spring Boot JPA, there are several ways to call stored procedures.使用Spring Boot JPA时,调用存储过程有几种方式。 A helpful set of annotations from Spring are available for working with stored procedures.
来自 Spring 的一组有用的注释可用于处理存储过程。
@Procedure
annotation help to map the Stored Procedure defined in entity class. @Procedure
注释帮助 map 实体 class 中定义的存储过程。
When used the @Procedure
annotation, got below exception org.springframework.dao.InvalidDataAccessApiUsageException: Type cannot be null; nested exception is java.lang.
当使用
@Procedure
注释时,出现以下异常org.springframework.dao.InvalidDataAccessApiUsageException: Type cannot be null; nested exception is java.lang.
org.springframework.dao.InvalidDataAccessApiUsageException: Type cannot be null; nested exception is java.lang.
I referred to the Spring Boot Stored Procedure Example to resolve above exception and to understand how to use the alternative solution with @Query
for stored procedure for select, update and delete with and without parameter.我参考了Spring Boot Stored Procedure Example来解决上述异常,并了解如何使用 @Query 的替代解决方案来存储
@Query
的存储过程,更新和删除带参数和不带参数。
public interface EmployeeRepository extends JpaRepository<Employee, Long> {
@Query(nativeQuery = true, value = "call get_employees")
List<Employee> getAllEmployees();
@Query(nativeQuery = true, value = "call get_employee_id(:emp_id)")
Employee getEmployeeById(@Param("emp_id") Long emp_id);
@Transactional
@Modifying
@Query(nativeQuery = true, value = "call delete_emp_id(:emp_id)")
void deleteEmployeeById(@Param("emp_id") Long emp_id);
Another way is using annotation @Procedure另一种方法是使用注解@Procedure
import org.springframework.data.jpa.repository.query.Procedure;
...
public interface PageCatalogRepository extends JpaRepository<PageCatalogEntity , String> {
@Procedure
public String firstProcedure(Long page_id);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.