Grails HQL QuerySyntaxException

时间:2014-08-21 00:50:28

标签: hibernate grails hql grails-2.0

我有以下HQL查询。当我运行它时,我得到了例外:

  

org.hibernate.hql.ast.QuerySyntaxException:意外令牌:on

我的代码如下。有人知道如何解决这个问题吗?

def results = LineItem.executeQuery("\
        select \
            li_ch.eventId as event_id, \
            sum(CASE li_ch.lineItemType WHEN :product THEN 1 WHEN :refund THEN (CASE li_p.lineItemType WHEN :product THEN -1 ELSE 0 END) ELSE 0 END) as sold, \
            sum(li_ch.amount) as gross \
        from \
            LineItem li_ch LEFT JOIN LineItem li_p on li_ch.parentLineItem.id = li_p.id, \
            Sale s \
        where \
            s.id = li_ch.sale.id AND \
            li_ch.eventId in (238141) AND \
            s.saleStatusType in (:paid, :refunded, :partialRefunded) AND \
            li_ch.lineItemType in (:product, :refund)  \
        group by li_ch.eventId", [product: LineItemType.PRODUCT, refund: LineItemType.REFUND, paid: SaleStatusType.PAID, refunded: SaleStatusType.REFUNDED, partialRefunded: SaleStatusType.PARTIAL_REFUND])

这给出了以下例外:

[2014-08-21 09:34:52,666] hql.PARSER line 1:371: unexpected token: on
[2014-08-21 09:34:52,685] errors.GrailsExceptionResolver QuerySyntaxException occurrwhen processing request: [GET] /inventory/events/salesData - parameters:
eventIds: 238141
unexpected token: on near line 1, column 371 [            select                 li_ch.eventId as event_id,                 sum(CASE li_ch.lineItemType WHEN :product THEN 1 WHEN :refund THEN (CASE li_p.lineItemType WHEN :product THEN -1 ELSE 0 END) ELSE 0 END) as sold,                 sum(li_ch.amount) as gross             from                 com.acme.inventory.domain.LineItem li_ch LEFT JOIN LineItem li_p on li_ch.parentLineItem.id = li_p.id,                 com.acme.inventory.domain.Sale s             where                 s.id = li_ch.sale.id AND                 li_ch.eventId in (238141) AND                 s.saleStatusType in (:paid, :refunded, :partialRefunded) AND                 li_ch.lineItemType in (:product, :refund)              group by li_ch.eventId]. Stacktrace follows:
org.hibernate.hql.ast.QuerySyntaxException: unexpected token: on near line 1, column 371 [            select                 li_ch.eventId as event_id,                 sum(CASE li_ch.lineItemType WHEN :product THEN 1 WHEN :refund THEN (CASE li_p.lineItemType WHEN :product THEN -1 ELSE 0 END) ELSE 0 END) as sold,                 sum(li_ch.amount) as gross             from                 com.acme.inventory.domain.LineItem li_ch LEFT JOIN LineItem li_p on li_ch.parentLineItem.id = li_p.id,                 com.acme.inventory.domain.Sale s             where                 s.id = li_ch.sale.id AND                 li_ch.eventId in (238141) AND                 s.saleStatusType in (:paid, :refunded, :partialRefunded) AND                 li_ch.lineItemType in (:product, :refund)              group by li_ch.eventId]
at com.acme.inventory.services.SaleRestService$$EOnaEzWy.eventSaleData(SaleRestService.groovy:15)
at com.acme.inventory.controllers.SaleRestController.eventSaleData(SaleRestController.groovy:13)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
at java.lang.Thread.run(Thread.java:695)

1 个答案:

答案 0 :(得分:0)

完整的例外可能会有所帮助,但我认为这与:

有关

我们不允许在JOIN声明中使用 ON 关键字。这是由映射驱动的。

我们可以做的是扩展生成的ON (generated by Hibernate based on mapping) - 但为此我们有一个关键字 WITH 。所以这应该有所帮助:

而不是:

// wrong
from
  LineItem li_ch LEFT JOIN LineItem li_p on li_ch.parentLineItem.id = li_p.id, 

// extend the JOIN condition using WITH
from
  LineItem li_ch LEFT JOIN LineItem li_p with li_ch.parentLineItem.id = li_p.id,