简体   繁体   中英

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

In my application I have a model that relates to itself in a parent/child relationship. Posts can have parents that are also posts. I've written a query to delete a target post and its descendants. When I execute the query outside of Spring it works perfectly. However when running it in Spring, the query executes successfully but throws the following exception:

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.

My query runs from an interface that extends 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);

I think you want to add the @Modifying annotation as well. See the documentation here . It's because SQL Delete does not return a resultset.

EDIT 1:

It comes down to execute (or executeQuery) vs executeUpdate if you're familiar with the JDBC API. Looks like Spring has the expectation that your method annotated with @Query will return a resultset and so it's disappointed when it doesn't. Related SO question/answer here .

如果@Query缺少@Modifying注释,请添加它。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM