Does Spring Data JPA have any way to count entites using method name resolving?

后端 未结 12 955
耶瑟儿~
耶瑟儿~ 2020-12-04 08:38

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

12条回答
  •  北荒
    北荒 (楼主)
    2020-12-04 09:31

    JpaRepository also extends QueryByExampleExecutor. So you don't even need to define custom methods on your interface:

    public interface UserRepository extends JpaRepository {
        // no need of custom method
    }
    

    And then query like:

    User probe = new User();
    u.setName = "John";
    long count = repo.count(Example.of(probe));
    

提交回复
热议问题