2017-06-18 56 views
1

我試着更新我的JPA庫更新查詢與JPA和Java

@Transactional 
public interface UserRepository extends JpaRepository<User, Integer> { 
User findByUsername(String username); 
User findById(Long id); 
@Query(value = "update user t set t.rule_id = NULL where t.rule_id = :id", nativeQuery = true) 
List<User> setNUll(@Param("id") String id);} 

這是我的控制器的一部分:

@RequestMapping(value = "/admin/rule/{id}/edit", method = RequestMethod.GET) 
public String editRule(@PathVariable Integer id, Model model) 
{ 
    userService.setNUll(Integer.toString(id)); 
    model.addAttribute("rule", ruleCrudService.getRuleById(id)); 
    updateUserData(); 
    return "ruleForm"; 
} 

而且在我的瀏覽器這個錯誤apears:

There was an unexpected error (type=Internal Server Error, status=500). could not extract ResultSet; nested exception is org.hibernate.exception.GenericJDBCException: could not extract ResultSet

服務器還說:

SQL Error: 0, SQLState: S1009 2017-06-18 12:51:15.778 ERROR 10388 --- [nio-8080-exec-4] o.h.engine.jdbc.spi.SqlExceptionHelper : Can not issue data manipulation statements with executeQuery(). 2017-06-18 12:51:15.844 ERROR 10388 --- [nio-8080-exec-4] o.a.c.c.C.[.[.[/].[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:不能使用executeQuery()發出數據操作語句。

回答

1

您需要使用@Modifying以上的註解@Query來使用JPA來更新查詢。