【问题标题】:Grails HQL QuerySyntaxExceptionGrails HQL QuerySyntaxException
【发布时间】:2014-08-21 00:50:27
【问题描述】:

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

org.hibernate.hql.ast.QuerySyntaxException:意外令牌:开启

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

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)

【问题讨论】:

  • 意外的令牌ON 就像我说的那样,对吗?问题是关键字 ON

标签: hibernate grails hql grails-2.0


【解决方案1】:

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

我们不允许在 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, 

【讨论】:

  • 我尝试将“on”切换为“with”,但出现了一个新异常:org.hibernate.hql.ast.QuerySyntaxException: Path expected for join!
  • 正是.. 因为现在我们确实有其他问题。但 ON 阻止我们看到这个错误。请正确查看此链接docs.jboss.org/hibernate/orm/3.3/reference/en/html/… 以查看 FROM 和 JOIN 的外观...
  • 感谢您的链接,但看起来我正在执行的连接类似于示例中的使用方式:from Cat as cat left join cat.kittens as kitten with kitten.bodyWeight > 10.0。你看到示例代码和我的代码有什么不同吗?
  • 问题出在这里:LineItem li_ch LEFT JOIN LineItem li_p,它应该包含与LineItem相关的LineItem路径。例如如果它是父子,它应该是:LineItem as li_ch LEFT JOIN li_ch.LineItem as li_p。见第二部分是 li_ch.LineItem。正如我在回答中所表达的那样:ON 子句是通过映射定义的 (它只能通过 WITH 扩展) 但这意味着,我们应该显示来自映射的关系。有帮助吗?
  • 我明白你的意思,感谢你的帮助。 li_ch.LineItem 虽然语法无效。
猜你喜欢
  • 2018-07-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多