在用Spring Boot JPA的时候,
@Query(value = "delete from search_vec where part = ?1 ", nativeQuery = true)
void dropByPart(int part);
导致异常:
Caused by: java.sql.SQLException: Statement.executeQuery() cannot issue statements that do not produce result sets.
解决方法:
在@Query上加上@Modifying,表示不需要返回值
@Modifying
@Query(value = "delete from search_vec where part = ?1 ", nativeQuery = true)
void dropByPart(int part);
这是因为,根据Spring data jpa官网
Doing so triggers the query annotated to the method as an updating query instead of a selecting one.
我猜测@Query对应到底层jdbc框架是Statement.executeQuery(),而@Modifying@Query对应的是Statement.execute();
在使用SpringBootJPA的@Query进行删除操作时,如果未添加@Modifying注解,会导致SQLException,因为默认执行的是查询语句而非修改语句。添加@Modifying后,系统会将查询注解的方法识别为更新操作,从而正确执行删除命令,避免了Statement.executeQuery()不适用于非查询语句的问题。
562

被折叠的 条评论
为什么被折叠?



