我有以下ERD:

java - 具有“sum”聚合的JPQL查询未按预期工作-LMLPHP

我想按区域选择员工薪资总和,但是我的选择语句无法按预期工作。

这是此查询的代码:

   @SuppressWarnings("unchecked")
public Salary findByRegion(String region) {
    return (Employee)  getEntityManager()
            .createQuery("select sum(e.employeeJob.salary) from Employee e where "
                    + "e.employeeDepartment.departmentLocation.locationCountry.countryRegion = :region")

            .setParameter("region", "%" + region + "%")
            .getSingleResult();
}


我在做什么错呢?

最佳答案

JPQL语句未提供预期结果的原因之一可能是路径表达式末尾缺少REGIONS实体的字段名称:e.employeeDepartment.departmentLocation.locationCountry.countryRegion而不是e.employeeDepartment.departmentLocation.locationCountry.countryRegion.region_name

10-06 02:14