2016-09-16 165 views
2

在我的應用程序中,我有一個與父/子關係本身相關的模型。帖子可以有也是帖子的父母。我寫了一個查詢來刪除一個目標文章及其後代。當我在Spring之外執行查詢時,它完美地工作。然而,在春季運行它時,查詢成功執行,但會引發以下異常:Spring Data JPA - 聲明沒有返回結果集

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); 

回答

4

我認爲你要添加的註釋@Modifying的界面運行以及。請參閱文檔here。這是因爲SQL Delete不返回結果集。

編輯1:

它歸結爲執行(或的executeQuery)VS executeUpdate的,如果你熟悉JDBC API。看起來Spring喜歡用@Query註解的方法返回一個結果集,所以它不會失望。相關SO問答here

+0

「@修改」與'@ Transactional'一起做了訣竅。非常感謝! –

+0

太棒了!樂於幫助。 – unigeek