【问题标题】:unexpected end of subtree JPQL子树JPQL的意外结束
【发布时间】:2018-12-21 15:09:36
【问题描述】:

我有一个查询,它计算 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);

【问题讨论】:

  • 尝试将 count(*) 替换为 count()
  • 我试过了,我得到了同样的错误,它没有改变任何东西

标签: java postgresql hibernate jpql


【解决方案1】:

您似乎需要将nativeQuery=true 添加到@Query 注释,否则JPA 无法理解没有from 的查询

【讨论】:

  • 谢谢你的回答,这正是我最终做的事情
猜你喜欢
  • 1970-01-01
  • 2010-12-27
  • 1970-01-01
  • 1970-01-01
  • 2011-01-05
  • 2014-03-27
  • 1970-01-01
  • 2011-06-03
  • 2019-12-25
相关资源
最近更新 更多