No results returned by the Query error in PostgreSQL
Please use @Modifying annotation over the @Query annotation.
@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 (i.e. DELETE, UPDATE or INSERT)
Use
executeUpdate
instead of
executeQuery
if no data will be returned (i.e. a non-SELECT
operation).
Using @Modifying and @Transaction fixed me
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. 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:
@Modifying
@Query(value = "DELETE FROM table t WHERE t.some_id IN (:someIds)", nativeQuery = true)
void deleteBySomeIdIn(@Param("someIds") Collection<Long> someIds);