簡體   English   中英

Spring Data JPA - 該語句沒有返回結果集

[英]Spring Data JPA - The statement did not return a result set

在我的應用程序中,我有一個在父/子關系中與自身相關的模型。 帖子可以有也是帖子的父母。 我寫了一個查詢來刪除目標帖子及其后代。 當我在 Spring 之外執行查詢時,它運行良好。 但是在 Spring 中運行時,查詢執行成功但拋出以下異常:

WARN  SqlExceptionHelper:144 - SQL Error: 0, SQLState: null
ERROR SqlExceptionHelper:146 - The statement did not return a result set.
ERROR [dispatcherServlet]:182 - 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
com.microsoft.sqlserver.jdbc.SQLServerException: The statement did not return a result set.

我的查詢從擴展 JpaRepository 的接口運行

@Query(value = "WITH ParentTree AS (\n" +
            "  SELECT Parent.post_key,\n" +
            "    1 AS Level\n" +
            "  FROM community_post AS Parent\n" +
            "    WHERE Parent.post_key = ?1\n" +
            "    UNION ALL\n" +
            "      SELECT Child.post_key,\n" +
            "        pt.Level + 1\n" +
            "      FROM community_post AS Child\n" +
            "      INNER JOIN ParentTree AS pt\n" +
            "        ON Child.post_parent = pt.post_key\n" +
            "      WHERE Child.post_parent IS NOT NULL\n" +
            ")\n" +
            "DELETE FROM community_post\n" +
            "  WHERE post_key IN (\n" +
            "      SELECT post_key\n" +
            "      FROM ParentTree\n" +
            "  )", nativeQuery = true)
    void recursiveDelete(long targetKey);

我認為您也想添加 @Modifying 注釋。 請參閱此處的文檔。 這是因為 SQL Delete 不返回結果集。

編輯 1:

如果您熟悉 JDBC API,那么它歸結為 execute(或 executeQuery)與 executeUpdate。 看起來 Spring 期望你用 @Query 注釋的方法會返回一個結果集,所以當它沒有時它會很失望。 相關的問題/答案在這里

如果@Query缺少@Modifying注釋,請添加它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM