Govind Singh
Govind Singh

Reputation: 15490

spring data jpa pagination with sub query not woking

below is the query which is working fine

 @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);

but when i pass sort param ?sort=name

then it is creating wrong 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 
com.pro.api.model.City as city order by cityTrans.name asc
                                        ^

can anyone tell why this is happening?

Upvotes: 2

Views: 1460

Answers (1)

Snekse
Snekse

Reputation: 15799

I think the MVC to JPA path only works for entity definitions, not Object[] results, so if your City entity doesn't have a name property, then JPA would try to find another field match, which would be the cityTrans

Upvotes: 2

Related Questions