【问题标题】:org.postgresql.util.PSQLException: No results returned by the queryorg.postgresql.util.PSQLException:查询没有返回结果
【发布时间】:2016-08-22 01:45:46
【问题描述】:

大家都在尝试在我的 Spring Boot 应用程序中使用 @Query 注释在表中插入数据,但出现 postgres 异常:

org.postgresql.util.PSQLException:  No results returned by the query 

这是我的代码:

这是存储库

@Query(value="INSERT INTO \"FCT_BY_DEV\"(\"IdDev\", \"IdFonction\") VALUES (?, ?) ",nativeQuery=true)
public String isertfonctionstodev(int dev,int fonction);

这是控制器:

@RequestMapping(value="/function/insert", method = RequestMethod.POST)
public String insererfonctions (int dev,int fonction){
    System.out.println("dev="+dev+"fonction="+fonction);
     fonctionRepository.isertfonctionstodev(dev, fonction);
     System.out.println("********");
     return "aaa";
 }

我在 angularJs 中通过 $http 使用此服务

$http.post("/function/insert?dev="+$scope.id+"&fonction="+$scope.idf);

最后这是服务器日志

dev=16006fonction=14
Hibernate: INSERT INTO "FCT_BY_DEV"("IdDev", "IdFonction") VALUES (?, ?) 
 2016-04-27 16:52:03.204  WARN 7036 --- [nio-8080-exec-2]    o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 0, SQLState: 02000
 2016-04-27 16:52:03.204 ERROR 7036 --- [nio-8080-exec-2] o.h.engine.jdbc.spi.SqlExceptionHelper   : Aucun résultat retourné par la requête.

数据是正确的,我用相同的值尝试了相同的查询,它为什么 posgres 会生成这个异常以及如何修复,感谢任何帮助

【问题讨论】:

  • 不确定您的 ORM 是如何工作的,但 INSERT 查询不应该返回任何内容(如记录集),除非您使用 RETURNING 子句。似乎您的 ORM 需要一个记录集。
  • 你的方法返回类型是String。你在这里期待什么?不应该是void吗?
  • 我刚刚在发布问题之前进行了编辑,我想也许我可以得到结果,因为它可以工作,但它既不是 void 也不是 String

标签: postgresql spring-boot


【解决方案1】:

我认为修改查询必须额外注释

@Modifying

【讨论】:

    【解决方案2】:

    这应该可以解决您的问题:

    @Transactional
    @Modifying(clearAutomatically = true)
    @Query(value="update policy.tbl_policy set ac_status = 'INACTIVE' where pol_id = :policyId and version_no = :version_no and ac_status = 'ACTIVE'", nativeQuery=true)
    public void updateExistingRowbyId(@Param("policyId") Long pol_id, @Param("version_no") Long version_no);
    

    如果没有@Transactional注解,可能会出现以下错误: javax.persistence.TransactionRequiredException:执行更新/删除查询

    【讨论】:

      猜你喜欢
      • 2015-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多