【问题标题】:How to resolve "No results were returned by the query" exception in Postgres while using JPA [duplicate]使用JPA时如何解决Postgres中的“查询未返回任何结果”异常[重复]
【发布时间】:2019-12-04 12:04:11
【问题描述】:

您好,我正在构建一个休息服务来使用 JPA 从 PostgreSQL 中删除一个实体。 代码是:

@Repository
public interface TestRepo  extends JpaRepository<Question, Long>{
    @Query(value = "delete from testmodel c where c.id in ?1 and c.name=?2",
            nativeQuery = true)
    void deleteModel(List<Long> ids, String text);
}

代码运行良好,条目已从 PostgreSQL 中删除,但我在终端中收到以下异常。

org.postgresql.util.PSQLException: No results were returned by the query.
at org.postgresql.jdbc.PgPreparedStatement.executeQuery(PgPreparedStatement.java:107) ~[postgresql-42.2.5.jar:42.2.5]
at com.zaxxer.hikari.pool.ProxyPreparedStatement.executeQuery(ProxyPreparedStatement.java:52) ~[HikariCP-3.2.0.jar:na]

当我搜索此异常时,我得到了使用 executeUpdate 而不是 executeQuery 的建议。

由于我使用的是 JPA,我不知道如何用 executeUpdate 替换 executeQuery

任何帮助都会很棒。

【问题讨论】:

  • 尝试添加@Modifying注解。

标签: java postgresql jpa spring-data-jpa


【解决方案1】:

您需要添加@Modifying 来声明它是一个更新查询,并且您不希望从数据库返回结果

@Repository
public interface TestRepo  extends JpaRepository<Question, Long>{
    @Modifying
    @Query(value = "delete from testmodel c where c.id in ?1 and c.name=?2",
            nativeQuery = true)
    void deleteModel(List<Long> ids, String text);
}

【讨论】:

    【解决方案2】:

    我认为修改查询必须用额外的注释 使用这个@Modifying,因为您正在修改查询并且不期望任何结果集

    【讨论】:

      猜你喜欢
      • 2011-04-17
      • 2020-07-09
      • 2018-10-02
      • 1970-01-01
      • 2021-05-25
      • 1970-01-01
      • 1970-01-01
      • 2017-05-20
      • 1970-01-01
      相关资源
      最近更新 更多