Custom delete method in JpaRepository

jpa repository delete
spring jpa delete multiple rows
spring data jpa bulk delete
spring data jpa delete by composite primary key
delete native query in spring data jpa
hibernate delete not working
how to delete record in spring boot
delete method in spring boot

I'd like to know if there's a way of overriding the delete method for some of my JpaRepository's without having to override the rest of the methods.

Currently I have something like

public interface UserRepo extends JpaRepository<User, Long>
    findUserById(long id);

    findUserByEmail(String email);

    // etc...

And I'd like to override the delete(T Entity) method in CrudRepository. To do so I've tried implementing the UserRepo but then I have to implement all the findByX and haven't really find anything around on how to properly do that.

Is there any annotation to add to a function in the Entity class so it runs when you call UserRepo.delete(myUser)?

Thanks in advance!

Not sure I understand you clear enough, but lets try:

... I have to implement all the findByX ...

You don't, spring will generate JPQL snippet if you name methods in your interface with suitable convection please take a look at this and this articles

... Is there any annotation to add to a function in the Entity class so it runs when you call UserRepo.delete(myUser)? ...

You can use @PreRemove / @PostRemove annotation on method in your entity class:

@PreRemove / @PostRemove
public void someMethod() { ... }

Spring Data JPA Delete and Relationships, Explore different ways to delete entities in Spring Data JPA. Among others, CrudRepository contains two methods: deleteById and deleteAll. Spring Data JPA interfaces like JpaRepository or PagingAndSortingRepository. When we need something more complex, we can write a custom query using  Persisting and deleting objects in JPA requires a transaction, that's why we should use a @Transactional annotation when using these derived delete queries, to make sure a transaction is running. This is explained in detail in the ORM with Spring documentation. 5. Custom Delete Query

In addition to Raheela Aslam post:

Spring-data documentation has an example of how you can override standard repository methods, for example:

interface CustomizedSave<T> {
  <S extends T> S save(S entity);

class CustomizedSaveImpl<T> implements CustomizedSave<T> {

  public <S extends T> S save(S entity) {
    // Your custom implementation

interface UserRepository extends CrudRepository<User, Long>, CustomizedSave<User> {

You can read about it there:

UPD: Read it carefully, because there are some important things, e.g The most important part of the class name that corresponds to the fragment interface is the Impl postfix.

Also the documentation says: Custom implementations have a higher priority than the base implementation and repository aspects.

Spring Data JPA, Learn how to define Spring Data deleteBy and removeBy methods. the JpaRepository interface and adding our derived methods to this class. Let's see the equivalent code for our derived delete methods, using a custom  Let's derive a method to delete Fruits by their name: @Repository public interface FruitRepository extends JpaRepository<Fruit, Long> { Long deleteByName(String name); } In this example, the deleteByName method returns the count of deleted records. Similarly, we can also derive a delete method of the form: List<Fruit> deleteByColor(String color);

Custom delete method in JpaRepository - spring - html, I'd like to know if there's a way of overriding the delete method for some of my JpaRepository's without having to override the rest of the methods. Currently I have  Spring JPA CrudRepository provides a default method to delete the records. Here are methods which are provided by CrudRepository. delete(T entity) deleting a record by Entry delete(ID id) delete a record by Primary Key ID.

In your case code will be like as below:

public interface UserRepo extends JpaRepository<User, Long>
    findUserById(long id);

    findUserByEmail(String email);

    // etc...

public interface UserRepositoryCustom {

    void deleteByEmail(String email);

public interface UserRepositoryImpl implements UserRepositoryCustom {

    public void deleteByEmail(String email) {
        //provide your custom implimentation

Spring Data JPA delete query, This blog post describes how you can add custom methods into all Spring Data JPA repositories. It returns an Optional which contains the deleted entity. If no entity import In this post, we will see How to write custom method in the repository in Spring Data JPA. We will cover the below points in this post. Also, we will see some basic rules to define query/repository method using different keywords.

If you want to keep Spring's behavior for deletion, but want to have some logic to be executed either before or after, you may utilize java8's interface default methods, and try the following :

public interface UserRepo extends JpaRepository<User, Long> {

    default void customDelete(User user) {
       // before logic
       // ..
       delete(user); // actual call to deletion
       // after logic
       // ..


Spring Data JPA Tutorial: Adding Custom Methods to All Repositories, We are finally ready to create our first Spring Data JPA repository. The void delete(T entity) method deletes the entity whose id is given as a Add a custom method to my repository and fetch the information of all rows. This ensures that Spring Data JPA won’t try to create an implementation for the BaseRepository interface. Add the deleteById () method to the created interface. This methods takes the id of the deleted entity as a method parameter and returns an Optional<T> object.

Spring Data JPA Tutorial: CRUD, Also following delete methods are already defined in CrudRepository package com.logicbig.example; import setSalary(salary); return e; } @Override public String toString() { return  In this article, we will show you how to add a custom method to Spring Data JPA CrudRepository and MongoDB MongoRepository. 1. CrudRepository. 1.1 Review a CustomerRepository, we will add a custom method to this repository.

Spring Data JPA - Derived Delete Queries, Using Streamable as Query Method Return Type; Returning Custom In addition to query methods, query derivation for both count and delete queries is available. import Spring Data JPA : Custom Repository / Query Methods Example June 8, 2014 by Amr Mohammed Leave a Comment In this tutorial i am going to explain how to use query methods and custom repository implementation in Spring Data JPA framework.

Spring Data JPA, This tutorial shows how we can use query methods to delete an entity (using deleteBy method name) with out writing any queries/DAO implementation code  Using this element looks up Spring Data repositories as described in Section 1.2.3, “Creating repository instances”.Beyond that it activates persistence exception translation for all beans annotated with @Repository to let exceptions being thrown by the JPA persistence providers be converted into Spring's DataAccessException hierarchy.

  • Please have a look into below issue somehow it related:…
  • Nope, that's not what I wanted to achieve.
  • Regarding the first one: If I implement the UserRepo then I have to implement all the methods I have there. So if I'm exposing a findByEmail in the UserRepo, then in UserRepoImpl where I could override the delete I must code the findByEmail. Regarding the @PreRemove/@PostRemove, I saw those but they are acting on EntityManager.remove() if I'm not mistaken. Will that work in UserRepo.delete(myUser)?
  • Ok, it seems that @PreRemove/@PostRemove work with the UserRepo.delete(myUser). I though they acted for the remove method on the EntityManager. Is that being used behind the scenes by JpaRepository? Anywhere to read more about it? Much thanks!
  • Take a look at this article:
  • Yeah, I saw that in the documentation but I really hoped there was an more simple way similar to the one proposed by @Kamil W. But I'll have to go that way if nothing better is possible :)
  • The thing is that I really need to override the delete method and do some stuff in the code before the User gets deleted so just SQL stuff may not do the whole trick.
  • Then you should do that in the service layer, not in the repository.
  • Then I'm afraid someone without much idea of what's happening will come and just call UserRepo.dele(myUser) and mess up with the data in the DB.
  • Then I guess JpaRepositories are not working out for you. They are there for basic CRUD operations, definitely not to add business logic.
  • Yeah, I know I can do that but is the delete(T entity) that I want to override to prevent an unaware developer from calling it instead of the deleteByEmail with custom behaviour and messing stuff because delete is not doing what it should but still deleting the Entity in the data base.
  • Ok Then you can create you custom Repository interface and then provide custom implementation.
  • That's what I also commented on the post. If I provide a custom implementation then I have to implement all the findByX methodfs too which is quite a bit of extra work and me messing with SQL more than I'd like too in that case :)
  • No you have to create UserRepository and UserRepositoryCustom in custom you can add methods that you want to provide custom implementation.