java - 如何使用 jpa 存储库获取多个平均值(spring boot)

标签 java spring spring-boot spring-data-jpa

基于线程:Spring Data JPA - Custom Query with multiple aggregate functions in result

我的 jpa 存储库中有此查询

@Query("SELECT new mx.com.sk.AveragesPojo(AVG(a.initial), AVG(a.initialEFSL), AVG(a.finalEFSL), AVG(a.entitySettlement)) FROM AveragesModel AS a WHERE groupName = :groupName AND idRemote = :idRemote")
    public AverajesPojo getLastSurveyAverages(@Param("groupName") String groupName, @Param("idRemote") Long idRemote);
}

在我的 pojo 构造函数中是:

public AverajesPojo(Float initial, Float initialEFSL, Float entitySettlement, Float finalEFSL) {
    super();
    this.initial = initial;
    this.initialEFSL = initialEFSL;
    this.entitySettlement = entitySettlement;
    this.finalEFSL = finalEFSL;
}

但是我有这个错误:

Error creating bean with name 'averagesRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Validation failed for query for method public abstract mx.com.sk.pojos.AverajesPojo mx.com.sk.admin.repositories.AveragesRepository.getLastSurveyAverages(java.lang.String,java.lang.Long)!

我的错误是什么?

最佳答案

请在您的 DTO 结构中使用 double 而不是 float,如果您想从 DTO 返回 float,那么您可以在您的结构中管理它。

The AVG function takes a state field path expression as an argument and calculates the average value of the sate field over the group. The state field must be numeric, and the result is returnd as a Double.

public class AveragesPojo {
    private double initial;
    private double initialEFSL;
    private double entitySettlement;
    private double finalEFSL;

    public AveragesPojo(double initial, double initialEFSL, double entitySettlement, double finalEFSL) {
        super();
        this.initial = initial;
        this.initialEFSL = initialEFSL;
        this.entitySettlement = entitySettlement;
        this.finalEFSL = finalEFSL;
    }

}

还请使用a.groupNamea.idRemote,并且返回类型应与构造相同,因为它们将返回相同的类型值,但它们将根据您的查询参数而定。所以让它们喜欢你的结构。 entitySettlementfinalEFSL:

@Query("SELECT new mx.com.sk.AveragesPojo(AVG(a.initial), AVG(a.initialEFSL), AVG(a.entitySettlement), AVG(a.finalEFSL )) FROM AveragesModel AS a WHERE a.groupName = :groupName AND a.idRemote = :idRemote")
public AverajesPojo getLastSurveyAverages(@Param("groupName") String groupName, @Param("idRemote") Long idRemote);
}

关于java - 如何使用 jpa 存储库获取多个平均值(spring boot),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52799135/

相关文章:

java - Spring OAuth2 隐式流重定向 url

java - 从字符串中分离出特定的单词

java - Apache solr.查询语法解释

java - Spring 启动: Disable fetch of PersistentBag programatically

java - 如何使用 MultiResourceItemReader 读取多个文件?

java - Spring @Transactional 只读

java - JpaRepository中通过自定义SQL排序方法排序

java - 从列表中删除重复的集

java - Proguard keepclassmembers 保持类(class)

java - 在两个Web应用程序之间共享Spring切面类