[英]how to retrieve an aggregation function query with spring-data-jpa
我正在使用Spring数据jpa 1.2,我无论如何都找不到像下面那样检索聚合查询结果。
select count(v), date(v.createTimestamp) from UserEntity v
group by date(v.createTimestamp)
哪个与原生JPA完美配合
@Entity()
public class UserEntity {
@Id
private long id;
.
.
@Column(columnDefinition = "TIMESTAMP DEFAULT CURRENT_TIMESTAMP")
private Timestamp createTimestamp;
}
我的JPA存储库是
public interface UserRepository extends JpaRepository<UserEntity, Long>,
JpaSpecificationExecutor<UserEntity> {
}
那么如何才能进行聚合查询抛出Spring数据,我在文档中找不到任何内容
我找到了一种方法来做到这一点
public interface UserRepository extends JpaRepository<UserEntity, Long>,
JpaSpecificationExecutor<UserEntity> {
@Query(value = "select count(v), date(v.createTimestamp) from UserEntity v group by date(v.createTimestamp)",
countQuery = "select count(1) from (select count(1) from UserEntity v group by date(v.createTimestamp)) z")
public List<Object[]> findCountPerDay();
}
这样我们就可以得到汇总数据和实际数量(汇总记录)
你也可以使用@NativeQuery
选项
你可以有@Query为春季数据的unsupporeted方法自定义查询。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.