子树JPQL意外结束

时间:2018-07-13 09:14:51

标签: java postgresql hibernate jpql

我有一个查询,该查询从4个表计数给定代码。我首先在postgresql中测试了此查询,它按预期工作,因此尝试将其翻译为JPQL并收到此错误:

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))]

我无法弄清楚什么是错误的,而Hibernate所说的“子树的意外末尾”是什么意思

PostgreSQL查询:

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

JPQL查询:

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

1 个答案:

答案 0 :(得分:0)

您似乎需要向nativeQuery=true注释中添加@Query,否则JPA无法在没有from的情况下进行查询