sunil banni
sunil banni

Reputation: 3

How to do soft delete using JPA query with spring api?

Hi I am a beginner in Java spring boot. I was trying to modify a particular record using :

adminRepo.updateAllActive(id);    // method Call

Repository:

@Query(value= "update admin a set a.active = 0 where a.id = :id " ,nativeQuery=true)
AdminEntity updateAllActive(@Param("id") Integer id);

But I am getting error "Could not extract Resultset"

Can anyone guide me for this. I am able to read entries from database.Issue only occurs when i try any modification query.

Thanks in advance.

Upvotes: 0

Views: 3168

Answers (3)

ledniov
ledniov

Reputation: 2382

Update query returns an integer value indicating the number of affected entries and can be overwritten to return void, so you should declare your method return type as void if you don't care about the count:

@Modifying
@Query(value= "update admin a set a.active = 0 where a.id = :id" ,nativeQuery=true)
void updateAllActive(@Param("id") Integer id);

Also you need to mark your query as @Modifying, for more info regarding update queries refer to documentation.

Upvotes: 2

Almas Abdrazak
Almas Abdrazak

Reputation: 3612

Example

  @Modifying
    @Query("Update Users u SET u.userKey=:userKey, u.phoneNumberVerified=true, u.name=:name, u.surname=:surname, u.phoneNumberVerified=true WHERE u.phoneCode=:phoneCode AND u.phoneNumber=:phoneNumber ")
    void updateUsers(@Param("userKey") String userKey, @Param("phoneCode") String phoneCode,
            @Param("phoneNumber") String phoneNumber, @Param("name") String name, @Param("surname") String surname);

Let me know if it correct

Upvotes: 0

Usagi Miyamoto
Usagi Miyamoto

Reputation: 6299

Add @Modifying annotation, and declare your method to return int.

See: https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.modifying-queries

Upvotes: 1

Related Questions