Does Spring Data JPA have any way of counting questions using method name resolution?

Spring JPA data supports object counting using specifications. But is there a way to count objects using method name resolution? Let's say I need the countByName method to count objects with a specific name, like the findByName method to retrieve all objects with a specific name.

+61
java spring spring-data-jpa
May 22 '12 at 5:59
source share
10 answers

As of Spring Data 1.7.1.RELEASE, you can do this in two different ways,

1) A new way, using query output for queries for both counting and deleting. Read this one (example 5). Example,

 public interface UserRepository extends CrudRepository<User, Integer> { Long countByName(String name); } 

2) Old way, Using @Query annotation.
Example,

 public interface UserRepository extends CrudRepository<User, Integer> { @Query("SELECT COUNT(u) FROM User u WHERE u.name=?1") Long aMethodNameOrSomething(String name); } 

or using the @Param annotation,

 public interface UserRepository extends CrudRepository<User, Integer> { @Query("SELECT COUNT(u) FROM User u WHERE u.name=:name") Long aMethodNameOrSomething(@Param("name") String name); } 
+81
Dec 13 '14 at 12:07
source share

Until you use version 1.4, you can use explicit annotation:

Example:

 @Query("select count(e) from Product e where e.area.code = ?1") int countByAreaCode(String code); 
+13
May 24 '13 at 7:48
source share

This feature was added in version 1.4 M1.

+8
May 13 '13 at 9:00 a.m.
source share

According to Abel, after version 1.4 (tested in version 1.4.3.RELEASE), you can do this:

public long countByName (string name);

+5
Jan 22 '14 at 15:29
source share

Working example

 @Repository public interface TenantRepository extends JpaRepository< Tenant, Long > { List<Tenant>findByTenantName(String tenantName,Pageable pageRequest); long countByTenantName(String tenantName); } 

DAO level call

 @Override public long countByTenantName(String tenantName) { return repository.countByTenantName(tenantName); } 
+3
Jun 23 '17 at 7:04 on
source share

Apparently it is now implemented by DATAJPA-231

+2
May 16 '13 at 14:39
source share

Thanks everyone! Now it works. DATAJPA-231

It would be nice if you could create an account ... By ... methods, how to find ... One by one. Example:

 public interface UserRepository extends JpaRepository<User, Long> { public Long /*or BigInteger */ countByActiveTrue(); } 
+1
Nov 03 '16 at 5:27
source share

JpaRepository also extends QueryByExampleExecutor. Therefore, you do not even need to define user methods on your interface:

 public interface UserRepository extends JpaRepository<User, Long> { // no need of custom method } 

And then a query like:

 User probe = new User(); u.setName = "John"; long count = repo.count(Example.of(probe)); 
+1
Feb 22 '17 at 1:27
source share

I have been working with him for only a few weeks, but I do not think that this is strictly possible, but you should get the same effect with less effort; just write the query yourself and annotate the method name. This is probably not much easier than writing a method yourself, but it is cleaner in my opinion.

0
May 26 '12 at 16:50
source share

According to the question of DATAJPA-231, this function has not yet been implemented.

0
Aug 11 2018-12-12T00:
source share



All Articles