8

Hi i am building a rest service to delete an entity from PostgreSQL using JPA. And the code is:

@Repository
public interface TestRepo  extends JpaRepository<Question, Long>{
    @Query(value = "delete from testmodel c where c.id in ?1 and c.name=?2",
            nativeQuery = true)
    void deleteModel(List<Long> ids, String text);
}

The code is working fine, the entries got deleted from PostgreSQL but i am getting the following exception in terminal.

org.postgresql.util.PSQLException: No results were returned by the query.
at org.postgresql.jdbc.PgPreparedStatement.executeQuery(PgPreparedStatement.java:107) ~[postgresql-42.2.5.jar:42.2.5]
at com.zaxxer.hikari.pool.ProxyPreparedStatement.executeQuery(ProxyPreparedStatement.java:52) ~[HikariCP-3.2.0.jar:na]

When i searched for this exception i got a suggestion to use executeUpdate instead of executeQuery.

Since i am using JPA i don't know how to replace executeQuery with executeUpdate

Any help would be greatful.

fphilipe
  • 9,739
  • 1
  • 40
  • 52
krishna
  • 343
  • 2
  • 5
  • 19

2 Answers2

23

You need to add @Modifying to declare that it is an update query and you don't expect a result back from the DB

@Repository
public interface TestRepo  extends JpaRepository<Question, Long>{
    @Modifying
    @Query(value = "delete from testmodel c where c.id in ?1 and c.name=?2",
            nativeQuery = true)
    void deleteModel(List<Long> ids, String text);
}
Elgayed
  • 1,129
  • 9
  • 16
3

I think modifying queries must be annotated with an extra use this @Modifying since u r modifying the query and not expecting any resultset

deHaar
  • 17,687
  • 10
  • 38
  • 51