2013-02-20 55 views
4

我正在使用Spring数据jpa 1.2,并且无法找到像以下这样的聚合查询结果。如何使用spring-data-jpa检索聚合函数查询

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> { 
} 

所以我怎么可以做一个汇总查询扔春季数据,我发现一无所有文档

回答

8

我发现了一个办法做到这一点

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(); 
} 

这样我们就可以用实际计数(汇总记录)

0

你可以有@Query为春季数据的unsupporeted方法自定义查询。

-1

相处累计资料,您还可以使用@NativeQuery选项