Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ??

like image 960
Ragesh Kr Avatar asked Jan 22 '14 06:01

Ragesh Kr


5 Answers

Use

executeUpdate

instead of

executeQuery

if no data will be returned (i.e. a non-SELECT operation).

like image 135
Paul Draper Avatar answered Nov 20 '22 08:11

Paul Draper


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)

like image 44
Dharmender Rawat Avatar answered Nov 20 '22 08:11

Dharmender Rawat


Using @Modifying and @Transaction fixed me

like image 21
Shahid Hussain Abbasi Avatar answered Nov 20 '22 07:11

Shahid Hussain Abbasi


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);
like image 26
JohnEye Avatar answered Nov 20 '22 09:11

JohnEye


If you want last generated id, you can use this code after using executeUpdate() method

 int update = statement.executeUpdate()
 ResultSet rs = statement.getGeneratedKeys();
 if (rs != null && rs.next()) {
  key = rs.getLong(1);
 }
like image 2
Popeye Avatar answered Nov 20 '22 08:11

Popeye