[英]No results returned by the Query error in PostgreSQL
我正在嘗試將數據插入表中。 執行查詢后,我收到一個異常說明
org.postgresql.util.PSQLException: No results were returned by the query.
org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:284)
數據已成功插入,但我不知道為什么會出現此異常??
用
executeUpdate
代替
executeQuery
如果沒有數據將被返回(即非SELECT
操作)。
請在@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);
任何 DML 查詢(即 DELETE、UPDATE 或 INSERT)也是如此
使用 @Modifying 和 @Transaction 修復了我
如果你想要最后生成的 id,你可以在使用 executeUpdate() 方法后使用此代碼
int update = statement.executeUpdate()
ResultSet rs = statement.getGeneratedKeys();
if (rs != null && rs.next()) {
key = rs.getLong(1);
}
使我想到這個問題的問題有點不同 - 使用基於接口的 Spring JPA 存儲庫刪除行時出現此錯誤。 原因是我的方法簽名應該返回一些結果:
@Modifying
@Query(value = "DELETE FROM table t WHERE t.some_id IN (:someIds)", nativeQuery = true)
List<Long> deleteBySomeIdIn(@Param("someIds") Collection<Long> someIds);
將返回類型更改為void
解決了問題:
@Modifying
@Query(value = "DELETE FROM table t WHERE t.some_id IN (:someIds)", nativeQuery = true)
void deleteBySomeIdIn(@Param("someIds") Collection<Long> someIds);
我已經使用addBatch
和executeBatch
解決了這個問題,如下所示:
statement.addBatch("DELETE FROM public.session_event WHERE id = " + entry.getKey());
statement.executeBatch();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.