Search code examples
spring-data

Spring Repository: get number of deleted rows


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?


Solution

  • 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.