Spring Repository: get number of deleted rows

11,126

Create a repository method with the @Modifying annotation as described here:

@Modifying
@Query("delete from data where createdAt < ?1")
int retainDataBefore(Date retainDate);

Return value gives you the count of deleted rows.

Share:
11,126
Etki
Author by

Etki

Ouchie, cagey person with no public bio here

Updated on June 13, 2022

Comments

  • Etki
    Etki almost 2 years

    I'm in need of getting count of deleted rows by Spring Repository custom query (i'm implementing basic external lock mechanism for application and is limited to MySQL database only). How can i achieve that?

  • maxxyme
    maxxyme over 2 years
  • P.Sanjay
    P.Sanjay about 2 years
    Use :1 instead of ?1. Like @Query("delete from DETAILS where ID = :1")