【问题标题】:Update Query with JPA and Java使用 JPA 和 Java 更新查询
【发布时间】:2017-11-20 21:28:16
【问题描述】:

我正在尝试更新我的 jpa 存储库

@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";
}

这个错误出现在我的浏览器中:

出现意外错误(类型=内部服务器错误,状态=500)。 无法提取结果集;嵌套异常是 org.hibernate.exception.GenericJDBCException: could not extract ResultSet

服务器还说:

SQL 错误:0,SQL 状态:S1009 2017-06-18 12:51:15.778 错误 10388 --- [nio-8080-exec-4] o.h.engine.jdbc.spi.SqlExceptionHelper:无法使用 executeQuery() 发出数据操作语句。 2017-06-18 12:51:15.844 错误 10388 --- [nio-8080-exec-4] oaccC[.[.[/].[dispatcherServlet]:Servlet.service() 用于 servlet [dispatcherServlet] path [] 抛出异常 [请求处理失败;嵌套异常是 org.springframework.orm.jpa.JpaSystemException: could not extract ResultSet;嵌套异常是 org.hibernate.exception.GenericJDBCException: could not extract ResultSet] with root cause

java.sql.SQLException: 无法使用 executeQuery() 发出数据操作语句。

【问题讨论】:

    标签: java hibernate model-view-controller spring-data-jpa


    【解决方案1】:

    您需要使用@Query 上面的@Modifying 注释来执行使用JPA 的更新查询。

    【讨论】:

      【解决方案2】:

      在我的例子中,我添加了两个注释。 @Transactional @修改 @Query 上面的注释使用 Jpa 执行更新查询。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-08-16
        • 2018-07-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多