1
votes

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().

  • Few things i understand, implementation of spring repository is provided by spring framework at runtime, So we cannot provide out own implementation. (This will overhead).
  • All methods in JPARepository is public only, so its obivious when we implement this interface all methods will be visible through out.
  • I am thinking of using DAO and Repository both at same time. DAO will provide custom function signature and repository will implement DAO interface.

Any Hack ?

2

2 Answers

1
votes

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.

0
votes

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