@Query(nativeQuery = true 报错
JavaWeb博客系统PowerBy SpringBoot+Vue.js+Nuxt.js 服务端(后端)第53课
UserDao中的
声明:
@Query(nativeQuery = true, value = "UPDATE `tb_user` SET `state` = '0' WHERE `id` = ?")
int deleteUserByState(String userId);
调用报错:
int result = iUserDao.deleteUserByState(userId);
Hibernate: insert into tb_refresh_token (create_time, refresh_token, token_key, update_time, user_id, id) values (?, ?, ?, ?, ?, ?)
Hibernate: UPDATE `tb_user` SET `state` = '0' WHERE `id` = ?
[2020/08/10-20:29:18] [http-nio-2020-exec-4] [WARN ] [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] : SQL Error: 0, SQLState: S1009
[2020/08/10-20:29:18] [http-nio-2020-exec-4] [ERROR] [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] : Can not issue data manipulation statements with executeQuery().
[2020/08/10-20:29:18] [http-nio-2020-exec-4] [ERROR] [org.apache.catalina.core.ContainerBase.[Tomcat].[localhost].[/].[dispatcherServlet]] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.orm.jpa.JpaSystemException: could not extract ResultSet; nested exception is org.hibernate.exception.GenericJDBCException: could not extract ResultSet] with root cause
java.sql.SQLException: Can not issue data manipulation statements with executeQuery().
只要是修改数据库,也就是删除/更新。你需要有事务。所以你的服务需要加@Transactional,不开启事务不让你干。另外你的是删除/更新操作,你加个 @Modifying注解。
祝你好运!