Is it possible to use COALESCE / NVL with selected entities inside HQL queries?
@Query(
SELECT DISTINCT COALESCE(
pi.provider.address.city.country,
pi.provider.address.alternateCountry
)
FROM PriceInfo pi
LEFT JOIN pi.article.categories cat
WHERE
pi.provider.objectStatus = :providerStatus AND
pi.article.objectStatus = :articleStatus AND
cat.objectStatus = :categoryStatus AND
(
(
pi.provider.address.city.objectStatus = :cityStatus AND
pi.provider.address.city.country.objectStatus = :countryStatus
)
OR (
pi.provider.address.city IS NULL AND
pi.provider.address.alternateCountry IS NOT NULL AND
pi.provider.address.alternateCountry.objectStatus
= :countryStatus
)
)
)
List<Country> havingArticles(
@Param("providerStatus") ObjectStatus providerStatus,
@Param("countryStatus") ObjectStatus countryStatus,
@Param("cityStatus") ObjectStatus cityStatus,
@Param("articleStatus") ObjectStatus articleStatus,
@Param("categoryStatus") ObjectStatus categoryStatus
);
The working version is here but it’s twice ugly as the one with COALESCE:
SELECT DISTINCT pi.provider.address.city.country
FROM PriceInfo pi
LEFT JOIN pi.article.categories cat
WHERE
pi.provider.objectStatus = :providerStatus
AND
pi.article.objectStatus = :articleStatus
AND
cat.objectStatus = :categoryStatus
AND
pi.provider.address.city.objectStatus = :cityStatus
AND
pi.provider.address.city.country.objectStatus = :countryStatus
UNION
SELECT DISTINCT pi.provider.address.alternateCountry
FROM PriceInfo pi
LEFT JOIN pi.article.categories cat
WHERE
pi.provider.objectStatus = :providerStatus
AND
pi.article.objectStatus = :articleStatus
AND cat.objectStatus = :categoryStatus
AND
pi.provider.address.city IS NULL
AND
pi.provider.address.alternateCountry IS NOT NULL
AND
pi.provider.address.alternateCountry.objectStatus = :countryStatus