简体   繁体   English

Spring Data JPA - 该语句没有返回结果集

[英]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.当我在 Spring 之外执行查询时,它运行良好。 However when running it in Spring, the query executes successfully but throws the following exception:但是在 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.

My query runs from an interface that extends JpaRepository我的查询从扩展 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.我认为您也想添加 @Modifying 注释。 See the documentation here .请参阅此处的文档。 It's because SQL Delete does not return a resultset.这是因为 SQL Delete 不返回结果集。

EDIT 1:编辑 1:

It comes down to execute (or executeQuery) vs executeUpdate if you're familiar with the JDBC API.如果您熟悉 JDBC API,那么它归结为 execute(或 executeQuery)与 executeUpdate。 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.看起来 Spring 期望你用 @Query 注释的方法会返回一个结果集,所以当它没有时它会很失望。 Related SO question/answer here .相关的问题/答案在这里

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring Data JPA 获取多个结果集 - Spring Data JPA get multiple result set 查询没有返回唯一结果:3; 嵌套异常是 javax.persistence.NonUniqueResultException: (Spring JPA Project) - query did not return a unique result: 3; nested exception is javax.persistence.NonUniqueResultException: (Spring JPA Project) Spring Data JPA无法使用复杂的表达式返回正确的结果 - Spring Data JPA fails to return correct result with complex expression 弹簧数据 jpa。 如果没有结果返回默认值,则查找最大值 - Spring data jpa. Find max if no result return default value 弹簧数据 jpa。 返回 Map 作为 group by 操作的结果 - Spring data jpa. Return Map as result of group by operation 如何在Spring数据JPA中获取结果集中的列名 - How to get column names in result set in Spring data JPA Spring Data JPA-返回未来 - Spring Data JPA - return Future Spring 数据 JPA 规范 - 返回列表并限制结果属性在列表中只出现一次 - Spring data JPA Specification - return list and limit result-attribute to occur only once in the list Spring Data JPA:当Collection类型的存储库方法参数为空时如何返回空结果? - Spring Data JPA: how to return empty result when repository method parameter of Collection type is empty? 更新和返回弹簧数据 JPA 中的数据 - update and return data in spring data JPA
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM