我创建了一个自定义的PostgreSQL函数:
create function delete_classes() returns void as
$$
DELETE FROM ...;
$$ language sql;
我想用Spring的JDBCTemplate调用这个函数。我尝试了以下方法:
@Repository
class Repository {
private final JdbcTemplate template; // initialized in constructor
public void deleteClasses() {
template.update("select delete_classes()");
}
}
我收到以下错误:
Caused by: org.springframework.dao.DataIntegrityViolationException:
PreparedStatementCallback; SQL [select delete_classes()];
A result was returned when none was expected.;
nested exception is org.postgresql.util.PSQLException:
A result was returned when none was expected.
PostgreSQL函数delete_classes
返回void
,所以我不确定这里的意思。 DELETE
语句没有RETURNING ...
子句。
执行该功能的正确方法是什么?
更新
我尝试了以下操作(execute
代替update
):
@Repository
class Repository {
private final JdbcTemplate template; // initialized in constructor
public void deleteClasses() {
template.execute("select delete_classes()", statement -> null);
}
}
这里,这实现了lambda-fashion中的PreparedStatementCallback
,它只返回null。
现在不调用函数delete_classes
。