【发布时间】:2020-12-01 15:28:27
【问题描述】:
我正在使用带有 MySQL 数据库的 Spring Data JPA。我有以下数据库表:
id points user_id
1 35 1
2 40 1
3 25 1
4 2500 2
我想获得points 列的SUM(),但不是使用本机查询。
在存储库中,我可以使用方法来COUNT() 行。当我尝试 sum 而不是 count 时,我得到一个错误。
public interface MyRepository extends JpaRepository<MyEntity, Long> {
public Long countPointsByUser(User user); // for user #1 returns 3
public Long sumPointsByUser(User user); // throws an exception, but I want return of 100 for user #1 and 2500 for user #2
}
Spring Data JPA 是否可以在没有@Query 注释的接口中编写方法?
【问题讨论】:
-
不可能的,可以阅读文档here。而且用@Query注解写JPQL很容易
-
你可以在这里寻找JPQL解决方案stackoverflow.com/questions/51637103/…
标签: spring spring-boot hibernate spring-data-jpa