How to Add Custom Method to Spring Data JPA

How to add custom method to Spring Data JPA

You need to create a separate interface for your custom methods:

public interface AccountRepository 
extends JpaRepository<Account, Long>, AccountRepositoryCustom { ... }

public interface AccountRepositoryCustom {
public void customMethod();
}

and provide an implementation class for that interface:

public class AccountRepositoryImpl implements AccountRepositoryCustom {

@Autowired
@Lazy
AccountRepository accountRepository; /* Optional - if you need it */

public void customMethod() { ... }
}

See also:

  • 4.6 Custom Implementations for Spring Data Repositories

  • Note that the naming scheme has changed between versions. See https://stackoverflow.com/a/52624752/66686 for details.

How to make a custom method in Spring Data JPA

You don't need to do this. The direct equivalent would be a method in your UserRepository interface defined as List<User> findByName(String name). You can also use JPA named queries, the @Query annotation, etc.

The usage of additional interfaces and implementation classes is intended for advanced use cases that are not (easily) possible in the queries Spring Data JPA generates for you based on method names, annotations, etc. It is not intended for easy use cases like the query you show.

And the reason why, is because that is how Spring Data JPA is designed. I highly recommend you read the full Spring Data JPA documentation. You'll notice that the solution in your question is just a minor part of the documentation, which makes clear that this is an escape hatch, not the primary way of using Spring Data JPA.

Add custom method to abstract class that implements another repository Interface to have both generic methods and custom methods in one place

You can achieve it in this way:

interface CustomPersonRepository {
void customMethod();
}

@Repository
class CustomPersonRepositoryImpl implements CustomPersonRepository { /* implementation */}

public interface PersonRepository
extends JpaRepository<Person , String>, CustomPersonRepository {
...
}

Note that PersonRepository extends CustomPersonRepository.

Now you can inject PersonRepository and it will have the implementations from CustomPersonRepositoryImpl

Implementing custom methods of Spring Data repository and exposing them through REST

The reason these methods are not exposed is that you're basically free to implement whatever you want in custom repository methods and thus it's impossible to reason about the correct HTTP method to support for that particular resource.

In your case it might be fine to use a plain GET, in other cases it might have to be a POST as the execution of the method has side effects.

The current solution for this is to craft a custom controller to invoke the repository method.

Can't create custom query method in Spring Data Repository

You are probably naming your implementation class wrong.

Note that the naming expectations changed with Spring Data 2.0.

For < 2.0 the implementation had to be named as the final repository interface with an additional Impl suffix. See the matching reference documentation for an example.

For >= 2.0 the implementation has to be named as the custom interface with an additional Impl suffix. See the current reference documentation for an example.

Note: You don't need any of the @Repository annotations.

Add Custom Method to JPARepository with Generics

You seem to be trying to add custom behaviour to a single repository, namely ProductRepository. However, the code is structured as if custom behaviour needs to be added to all repositories (CustomRepository is not specific to Products). Hence, the error.


Step 1: Declare a custom repository

interface CustomRepository<T, ID extends Serializable> {
void customMethod();
}

Step 2: Add custom behaviour to the required repository

interface ProductRepository extends CrudRepository<Product, Integer>
, CustomRepository<Product, Integer> {}

Step 3: Add a Product specific implementation

class ProductRepositoryImpl implements CustomRepository<Product, Integer> { ... }

Note: The class must be named ProductRepositoryImpl for Spring Data JPA plumbing to pick it up automatically.


A working example is available on Github.


If you would rather add custom behaviour to all repositories, refer to the relevant section in the official documentation.



Related Topics



Leave a reply



Submit