When I run this query, I experience n+1 issue where data JDBC fetches all the related entities of the campaign objects. Is there any way to avoid this using data JDBC?
@Query(
"""
SELECT campaign.*
FROM campaign
JOIN targeting ON campaign.targeting_id = targeting.id
WHERE (
CASE
WHEN campaign.applications_close_date IS NOT NULL
THEN NOW() BETWEEN campaign.start_date AND campaign.applications_close_date
WHEN campaign.end_date IS NOT NULL
THEN NOW() BETWEEN campaign.start_date AND campaign.end_date
ELSE NOW() >= campaign.start_date
END
)
AND NOT EXISTS
(
SELECT *
FROM application
WHERE application.campaign = campaign.id
AND application.influencer = :influencerId
)
"""
)
fun findAllMatchingByInfluencerId(
influencerId: Long,
country: String?
): List<Campaign>