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
As of Spring Data 1.7.1.RELEASE you can do it with two different ways,
1) The new way, using query derivation for both count and delete queries. Read this, (Example 5). Example,
public interface UserRepository extends CrudRepository {
Long countByName(String name);
}
2) The old way, Using @Query annotation.
Example,
public interface UserRepository extends CrudRepository {
@Query("SELECT COUNT(u) FROM User u WHERE u.name=?1")
Long aMethodNameOrSomething(String name);
}
or using @Param annotation also,
public interface UserRepository extends CrudRepository {
@Query("SELECT COUNT(u) FROM User u WHERE u.name=:name")
Long aMethodNameOrSomething(@Param("name") String name);
}
Check also this so answer.