我有这个hql查询。
Query query = session.createQuery(
"select inv from Invoice inv " +
"where 1=1 " +
"and inv.edited = false " +
"and businessSubscription.customer.user.id = :userId " +
"and inv.customerInvoice is null " +
"and inv.parentInvoice is null order by inv.id desc " +
"and exists (select 1 from InvoiceItem invItm where invItm.invoice.id = inv.id and invItm.product.id in (:productIdList))"
);
为什么会收到此异常?
org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: and near line 1, column 228 [select inv from Invoice inv where 1=1 and inv.edited = false and businessSubscription.customer.user.id = :userId and inv.customerInvoice is null and inv.parentInvoice is null order by inv.id desc and exists (select 1 from InvoiceItem invItm where invItm.invoice.id = inv.id and invItm.product.id in (:productIdList))]
at org.hibernate.hql.internal.ast.QuerySyntaxException.convert(QuerySyntaxException.java:91)
at org.hibernate.hql.internal.ast.ErrorCounter.throwQueryException(ErrorCounter.java:109)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:304)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:203)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:158)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:126)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:88)
at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:167)
at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:301)
at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:236)
at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1800)
语法错误在哪里?
我认为问题出在invItm.invoice.id = inv.id
上,可以在括号中使用外部别名吗?
答案 0 :(得分:1)
您会收到此异常(unexpected token: and near line 1, column 228
),因为您在and exists
之后有order by inv.id desc
。将exists
移到您的子句中(在order by
之前)。