如何使用Spring的JDBCTemplate执行PostgreSQL函数?
我创建了一个自定义PostgreSQL函数:如何使用Spring的JDBCTemplate执行PostgreSQL函数?,sql,spring,postgresql,jdbctemplate,Sql,Spring,Postgresql,Jdbctemplate,我创建了一个自定义PostgreSQL函数: create function delete_classes() returns void as $$ DELETE FROM ...; $$ language sql; 我想用Spring的JDBCTemplate调用这个函数。我尝试了以下方法: @Repository class Repository { private final JdbcTemplate template; // initialized in constru
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
):
这里,它以lambda方式实现了一个PreparedStatementCallback
,它只返回null
现在没有调用函数
delete\u classes
。不要使用update()
使用execute()
我试过了,但我不确定如何实现PreparedStatementCallback
回调,因为我不需要任何结果。见问题更新。
@Repository
class Repository {
private final JdbcTemplate template; // initialized in constructor
public void deleteClasses() {
template.execute("select delete_classes()", statement -> null);
}
}