【问题标题】:Delete all messages from the database从数据库中删除所有消息
【发布时间】:2020-01-08 08:10:00
【问题描述】:

我想要一个 cURL 请求来删除我存储在 MySQL 数据库中的所有消息,所以我编写了一个查询,当我在命令行中输入它时,它会显示一个错误。这是我的 cURL 请求

curl -X DELETE "Content-Type: application/json" localhost:8080/api/delete

这是终端中的这样一个错误

{"timestamp":"2020-01-08T08:09:06.521+0000","status":500,"error":"Internal Server Error","message":"could not extract ResultSet; nested exception is org.hibernate.exception.GenericJDBCException: could not extract ResultSet","trace":"org.springframework.orm.jpa.JpaSystemException: could not extract ResultSet;

我的代码

@DeleteMapping("/api/delete")
    public String clearBase() {
        messageService.deleteMessages();
        return "Все сообщения были удалены";
    }
}

消息库

@Query(value = "DELETE * FROM chatMessages", nativeQuery = true)
    void clearBase();

【问题讨论】:

  • 已经有一个可以使用的deleteAll 方法。无需自己编写。改用那个。如果您真的想要查询,您需要添加 @Modifying 并修复查询它应该是 DELETE FROM chatMessages 没有 *
  • 不是答案,但 DELETE * 是伪造的 SQL。应该是DELETE FROM chatMessages

标签: java mysql spring spring-boot curl


【解决方案1】:

您必须指定您将执行更新。 添加@Modifying 作为方法注解。

查看此链接:https://www.baeldung.com/spring-data-jpa-query

【讨论】:

  • 也许@Transactional 也可以使用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-20
  • 1970-01-01
  • 2012-05-22
  • 2017-06-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多