How to get aggregation function request using spring -data-jpa

I am using Spring jpa 1.2 data, and I still cannot find the result of the generic query, like the following.

select count(v), date(v.createTimestamp) from UserEntity v group by date(v.createTimestamp) 

What works great with embedded JPA

 @Entity() public class UserEntity { @Id private long id; . . @Column(columnDefinition = "TIMESTAMP DEFAULT CURRENT_TIMESTAMP") private Timestamp createTimestamp; } 

any my jpa repository

 public interface UserRepository extends JpaRepository<UserEntity, Long>, JpaSpecificationExecutor<UserEntity> { } 

since I can execute aggregate throw spring data queries, I don't see anything in the documentation

+4
source share
4 answers

I found a way to do this

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

Thus, we can obtain aggregate data together with the actual account (aggregated records)

+8
source

You may have @Query for custom queries for unsupported methods in spring-data.

0
source

you can also use the @NativeQuery option

-1
source

All Articles