Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring: Override save method in CrudRepository

I would like to add some code in save method in CrudRepository but keep orginal functionality. After that save method should do what before, save to repository but additionally execute extra action. Is it possible? If so, how could I do it properly?

Thank you in advance for any tips

Edit

So, I would like to create method like this:

@Override
public default <S extends MyClass> S save(S myClassItem) {
    //here my functionality 
    super.save(myClassItem); //from CrudRepository
    return myClassItem;
}
like image 881
adolzi Avatar asked Dec 18 '15 13:12

adolzi


1 Answers

I don't think it's possible to user super in such a method (default and overridden). So you must change the name of the method - which might not be exactly what you are looking for :

public interface TestRepository extends CrudRepository<MyClass, String> {

    default MyClass mySave(MyClass myClassItem) {
        save(myClassItem);
        return myClassItem;
    }
}
like image 139
Benoit Avatar answered Sep 23 '22 19:09

Benoit