Jivesh Relhan
Jivesh Relhan

Reputation: 60

How to hide spring data repository functions in service class?

I am using spring data JPA repository, my requirement is when i call repository class methods in service class it should show only custom methods like addUser(X,Y) instead of save().

Any Hack ?

Upvotes: 2

Views: 1812

Answers (2)

Jens Schauder
Jens Schauder

Reputation: 81940

If you don't want the methods from JpaRepository or CrudRepository, don't extend those but just Repository instead. It is perfectly fine to have a repository interface like

MyVeryLimitedRepository extends Repository<User, Long> {
     User findByName(String name);
}

Of course methods like addUser(X,Y) will need a custom implementation.

Upvotes: 2

shankarsh15
shankarsh15

Reputation: 1967

You can very well use DAO pattern in this case .

By implementing DAO Pattern in Service Class

  1. You create a wrapper between Service and Repository.

  2. You can custom code your DAO layer to only expose custom methods to service layer

Upvotes: 0

Related Questions