简体   繁体   English

PostgreSQL 中的查询错误没有返回任何结果

[英]No results returned by the Query error in PostgreSQL

I am trying to insert a data into a table.我正在尝试将数据插入表中。 After executing the query i am getting an exception stating执行查询后,我收到一个异常说明

org.postgresql.util.PSQLException: No results were returned by the query.
org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:284)

The data is getting inserted successfully, but i have no idea why i am getting this exception ??数据已成功插入,但我不知道为什么会出现此异常??

Use

executeUpdate

instead of代替

executeQuery

if no data will be returned (ie a non- SELECT operation).如果没有数据将被返回(即非SELECT操作)。

Please use @Modifying annotion over the @Query annotion.请在@Query注释上使用@Modifying注释。

@Modifying
@Query(value = "UPDATE Users set coins_balance = coins_balance + :coinsToAddOrRemove where user_id = :user_id", nativeQuery = true)
    int updateCoinsBalance(@Param("user_id") Long userId, @Param("coinsToAddOrRemove") Integer coinsToAddOrRemove);

The same is true for any DML query (ie DELETE, UPDATE or INSERT)任何 DML 查询(即 DELETE、UPDATE 或 INSERT)也是如此

使用 @Modifying 和 @Transaction 修复了我

If you want last generated id, you can use this code after using executeUpdate() method如果你想要最后生成的 id,你可以在使用 executeUpdate() 方法后使用此代码

 int update = statement.executeUpdate()
 ResultSet rs = statement.getGeneratedKeys();
 if (rs != null && rs.next()) {
  key = rs.getLong(1);
 }

The problem that brought me to this question was a bit different - I was getting this error when deleting rows using an interface-based Spring JPA Repository.使我想到这个问题的问题有点不同 - 使用基于接口的 Spring JPA 存储库删除行时出现此错误。 The cause was that my method signature was supposed to return some results:原因是我的方法签名应该返回一些结果:

@Modifying
@Query(value = "DELETE FROM table t WHERE t.some_id IN (:someIds)", nativeQuery = true)
List<Long> deleteBySomeIdIn(@Param("someIds") Collection<Long> someIds);

Changing the return type to void resolved the issue:将返回类型更改为void解决了问题:

@Modifying
@Query(value = "DELETE FROM table t WHERE t.some_id IN (:someIds)", nativeQuery = true)
void deleteBySomeIdIn(@Param("someIds") Collection<Long> someIds);

I have solved this Problem using addBatch and executeBatch as following:我已经使用addBatchexecuteBatch解决了这个问题,如下所示:

statement.addBatch("DELETE FROM public.session_event WHERE id = " + entry.getKey());
statement.executeBatch();

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

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