【发布时间】:2021-09-02 21:10:00
【问题描述】:
PostgreSQL 程序:
CREATE OR REPLACE PROCEDURE public.create_task_by_device(task_id integer, device_ids text)
LANGUAGE plpgsql
AS $procedure$
begin
-- do something here
END;
$procedure$
;
Spring JPA:(Kotlin)
interface TaskTemplateRepository : JpaRepository<TaskTemplate, Int> {
@Procedure("public.create_task_by_device")
fun createTaskByDevice(@Param("task_id") taskId: Int, @Param("device_ids") device_ids: String)
}
例外:
org.postgresql.util.PSQLException: ERROR: public.create_task_by_device(integer, character varying) is a procedure. To call a procedure, use CALL.
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2553) ~[postgresql-42.2.20.jar:42.2.20]
然后我得到spring.jpa.show-sql: true执行的SQL语句{call public.create_task_by_device(?,?)}
看来实际执行的语句是call using call。
不明白为什么整句话都被{ ... }包围了
然后我尝试单步调试,org.postgresql.jdbc.PgStatement#execute方法中实际执行的SQL语句是select * from create_task_by_device(1,'2') as result,不像JPA在命令行输出的SQL语句,不知道为什么
最后,我尝试编写自己的 SQL 语句
@Query("call public.create_task_by_device(?1, ?2)", nativeQuery = true)
fun createTaskByDevice(@Param("task_id") taskId: Int, @Param("device_ids") device_ids: String)
我遇到了另一个异常org.postgresql.util.PSQLException: No results were returned by the query.
如果我在方法中添加@Modifying,则抛出javax.persistence.TransactionRequiredException: Executing an update/delete query
【问题讨论】:
标签: spring postgresql spring-data-jpa spring-data