How to resolve "No results were returned by the query" exception in Postgres while using JPA

krishna :

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.

Elgayed :

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);
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=125782&siteId=1