2013-02-20 54 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選項