Spring Data JPA supports counting entities using specifications. But does it have any way to count entities using method name resolving? Let\'s say I want a method cou
According to Abel, after the version 1.4 (tested in version 1.4.3.RELEASE) is possible doing this way:
public long countByName(String name);
@Autowired
private UserRepository userRepository;
@RequestMapping("/user/count")
private Long getNumberOfUsers(){
return userRepository.count();
}
JpaRepository also extends QueryByExampleExecutor. So you don't even need to define custom methods on your interface:
public interface UserRepository extends JpaRepository<User, Long> {
// no need of custom method
}
And then query like:
User probe = new User();
u.setName = "John";
long count = repo.count(Example.of(probe));
I have only been working with it for a few weeks but I don't believe that this is strictly possible however you should be able to get the same effect with a little more effort; just write the query yourself and annotate the method name. It's probably not much simpler than writing the method yourself but it is cleaner in my opinion.
Edit: it is now possible according to DATAJPA-231
According to the issue DATAJPA-231 the feature is not implemented yet.
Apparently it is implemented now DATAJPA-231