1

I am using spring boot 2.1.2 and have a CustomerRepository which extends CrudRepository, now to override the save method (to do something before save is called) I have created a CustomerInterface and extended CustomerRepository. Now on the CustomerRepository I have @Repository. What should be the annotation for CustomerInterface.

I have tried @Transactional, but is that best in use here ?

@Repository
public interface CustomerRepository extends CrudRepository<Customer, Integer> {
    Customer save(Customer customer);
}



public interface PolicyStatusDao extends PolicyIdRepository {
    default void extractReport(Customer customer) {
        // do something
        save(customer);
    }
}

Note: Ignore the naming, its just an example. I wanted every class and interface to be annotated with spring (or other annotations like lombok javax etc) wherever required, hence the question.

devfreak
  • 271
  • 1
  • 3
  • 11
  • Check [this](https://www.baeldung.com/spring-data-jpa-query) – CodeMatrix Apr 26 '19 at 08:19
  • I would say @Transactional is the right one but you should not extend your CustomerRepository, but make the processing in your service class (or where the save is called). BTW you dont have to define explicitly the save method in your Repository, it is already in the parent Interface. – BackToBasics Apr 26 '19 at 08:57
  • 1
    Possible duplicate of [Spring Data: Override save method](https://stackoverflow.com/questions/13036159/spring-data-override-save-method) –  Apr 26 '19 at 09:30
  • You don't need an annotation. You, generally speaking, shouldn't place annotations on interfaces. The `@Repository` is also useless here as it is ignored as Spring Data has different ways of detecting classes. – M. Deinum Apr 26 '19 at 09:51

1 Answers1

0

You can try this to override SAVE

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

class CustomizeSaveImpl<T> implements CustomizeSave<T> {

  public <S extends T> S save(S entity) {
    // Your implementation goes here. Customize 
  }
}

Here is the Customized repository interfaces

interface CustomerRepository extends CrudRepository<Customer, Integer>, CustomizedSave<Customer> {
}
Rowi
  • 545
  • 3
  • 9
  • Your `CustomerRepository` handles `Customer` and `User` entities? That's wrong! –  Apr 26 '19 at 09:25