spring数据jpa与子查询分页而不是woking

时间:2017-12-04 15:24:06

标签: java sql spring-boot spring-data-jpa hql

下面是查询工作正常

 @Query("SELECT city.id as id,city.latitude as latitude, city.longitude as longitude, \n" +
        "(SELECT cityTrans.name " +
        "   FROM city.listOfCityTrans as cityTrans " +
        "   WHERE cityTrans.languageId=:languageId AND cityTrans.cityId=city.id)" +
        " AS name, \n" +
        "(SELECT provinceTrans.name " +
        "   FROM city.province AS province JOIN province.listOfProvinceTrans AS provinceTrans " +
        "   WHERE provinceTrans.languageId=:languageId AND provinceTrans.provinceId=city.provinceId)" +
        " AS provinceName, \n" +
        "(SELECT countryTrans.name " +
        "   FROM city.province AS province JOIN province.country.listOfCountryTrans AS countryTrans " +
        "   WHERE countryTrans.languageId=:languageId AND countryTrans.countryId=city.provinceId)" +
        " AS countryName, province.id as provinceId,province.countryId as countryId \n" +
        " FROM City as city")
Page<CitySummary> findCities(@Param("languageId") Integer languageId, Pageable pageable);

但是当我通过排序参数?sort=name

然后它正在创建错误的查询

SELECT city.id as id,city.latitude as latitude, city.longitude as longitude, 
\n(SELECT cityTrans.name    FROM city.listOfCityTrans as cityTrans    WHERE 
cityTrans.languageId=:languageId AND cityTrans.cityId=city.id) AS name, 
\n(SELECT provinceTrans.name    FROM city.province AS province JOIN 
province.listOfProvinceTrans AS provinceTrans    WHERE 
provinceTrans.languageId=:languageId AND 
provinceTrans.provinceId=city.provinceId) AS provinceName, \n(SELECT 
countryTrans.name    FROM city.province AS province JOIN 
province.country.listOfCountryTrans AS countryTrans    WHERE 
countryTrans.languageId=:languageId AND 
countryTrans.countryId=city.provinceId) AS countryName, province.id as 
provinceId,province.countryId as countryId \n FROM 
com.pro.api.model.City as city order by cityTrans.name asc
                                        ^

有人能说出为什么会这样吗?

1 个答案:

答案 0 :(得分:2)

我认为MVC到JPA路径仅适用于实体定义,而不适用于Object[]结果,因此如果您的City实体没有name属性,则JPA会尝试找到另一个字段匹配,即cityTrans