Search code examples
javapostgresqlhibernatejpql

unexpected end of subtree JPQL


I have a query which count a given code from 4 tables. I tested this query first in postgresql, it worked as I expected so I tried to translate it to JPQL and I got this error :

java.lang.IllegalArgumentException: 
org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected end of 
subtree [ select ((select count(*) from *.Record r where 
r.codeCampaign =?1 ) +  (select count(*) from *.AccountAssociationAction aaa where aaa.codeCampaign = ?1) + 
(select count(*) from *.CampaignPayment cp where cp.pk.campaignCode = ?1) + (select count(*) from 
*.ActionPeriodDepartment apd
where apd.id.codeCampaign = ?1))]

I can't figure out what is wrong and what does Hibernate mean by "unexpected end of subtree"

The postgresql query :

select  (select count(*) from account_association_action aaa where 
aaa.code_campaign = 'CAMP01') + (select count(*) from _campaign_payment cp 
where cp.campaign_code = 'CAMP01') + (select count(*) from record r where 
r.code_campaign ='CAMP01') + (select count(*) from action_period_department apd 
where apd.code_campaign = 'CAMP01'); 

the JPQL query :

@Query(value=" select (select count(*) from Record r where r.codeCampaign =?1 ) + " +
" (select count(*) from AccountAssociationAction aaa where aaa.codeCampaign = ?1) +" +
" (select count(*) from CampaignPayment cp where cp.pk.campaignCode = ?1) +" +
" (select count(*) from ActionPeriodDepartment apd where apd.id.codeCampaign = ?1)")
int countCampaignCodeUses(String campaignCode);

Solution

  • It looks like you need to add nativeQuery=true to @Query annotation otherwise JPA is failing to make sense of a query without from