2

We can write custom implementation of repository:

interface UserRepositoryCustom {
  public void someCustomMethod(User user);
}

class UserRepositoryImpl implements UserRepositoryCustom {

  public void someCustomMethod(User user) {
    // Your custom implementation
  }
}

But what if I want customize only some methods? For example:

interface UserRepositoryCustom {
  public User findByFirstName(String firstName);

  @Query("select u.firstName from User u where u.age > 18")
  public Set<String> findAllAdultUsers();

  public void someCustomMethod(User user);
}

class UserRepositoryImpl implements UserRepositoryCustom {
  //I want implement only this method
  public void someCustomMethod(User user) {
    // Your custom implementation
  }
}

If I declare a class, which implements an interface, I have to implement all methods, but I want to write custom logic for only one method. Is it possible to do this? Maybe I can make this class abstract? Will spring data resolve this?

Korobko Alex
  • 816
  • 1
  • 7
  • 10
  • See if [this post](http://stackoverflow.com/questions/28361275/how-to-reference-the-normal-spring-data-repo-from-a-custom-implementation/28368972#28368972) helps. – manish Jul 12 '15 at 12:10

2 Answers2

2

I think only solution is to split methods in 2 interfaces: first - for spring query method, and second - for custom implementation, as shows in doc: http://docs.spring.io/spring-data/data-jpa/docs/current/reference/html/#repositories.single-repository-behaviour.

But I think solution with abstract class would be more natural and logic: you provide only needed method implementations and spring data do the rest for you.

Michal Borek
  • 4,584
  • 2
  • 30
  • 40
Korobko Alex
  • 816
  • 1
  • 7
  • 10
0

Let's say you have a repository with a few methods whose implementations are generated by Spring:

interface UserRepository extends CrudRepository<User,String> {
    List<User> findUserByLastname(String lastName);
}

In order to add a method with a custom implementation, you need to create another interface that only contains the methods you want to customize, and make your repository extend the custom one:

interface CustomUserRepository {
    User someCustomMethod();
}

interface UserRepository extends CrudRepository<User,String>, CustomUserRepository {
    List<User> findUserByLastname(String lastName);
}

You can then implement the extra methods by creating an implementation class for the new interface:

class CustomUserRepositoryImpl implements CustomUserRespository {
    @Override
    User someCustomMethod() {
        // implementation goes here.
    }
}

The class name is important here: in order for Spring to find it, it should be the name of the interface that is being extended with Impl on the end.

The implementation repository is a normal Spring bean, so you can autowire a constructor to inject various dependencies.

There is a much more detailed tutorial here: https://www.baeldung.com/spring-data-composable-repositories.

Soupy
  • 166
  • 2
  • 6