Spring data JPA @Query mapping with named columns

巧了我就是萌 提交于 2019-12-02 13:02:47

How about using Projections as below?

static interface VehicleStats { 
    public String getSourceModule();
    public Long getVehicleCount();
}

And your repository method would be

@Query("select v.sourceModule as sourceModule, count(v) as vehicleCount from Vehicle v group by v.sourceModule")
List<VehicleStats> sourceModuleStats();

In your Service class, you can use the interface methods as below.

List<VehicleStats> objects = vehicleRepository.sourceModuleStats();
return objects.stream()
        .map(o->SourceModuleStatDTO.from(getSourceModule(),getVehicleCount() )
        .collect(Collectors.toList());
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!