我有以下ERD:
我想按地区选择员工工资组的总和,但我的选择语句不能按预期工作。
以下是此查询的代码:
@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();
}
我做错了什么?
答案 0 :(得分:0)
您的JPQL语句没有给出预期结果的原因之一可能是路径表达式末尾的REGIONS实体缺少字段名称:e.employeeDepartment.departmentLocation.locationCountry.countryRegion
而不是e.employeeDepartment.departmentLocation.locationCountry.countryRegion.region_name
答案 1 :(得分:-1)
如果您想使用“%”,则必须使用“ LIKE ”运算符而不是“ = ”。使用以下代码更改您的代码:
@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 LIKE :region")
.setParameter("region", "%" + region + "%")
.getSingleResult();
}