【问题标题】:Hibernate Query Language in grailsgrails 中的休眠查询语言
【发布时间】:2012-06-26 10:02:09
【问题描述】:

我目前正在处理一个需要执行休眠查询的 grails 项目。每次我使用数据库上的现有数据运行代码时,一切正常。但是,如果我在数据库为空的情况下运行它,则会引发错误。

这是我的代码:

    def actActivation = AuditLog.executeQuery("select ll.username from AuditLog ll where ll.event = ? ",[AuditLogEvent.ACCOUNT_ACTIVATION])
    def actDeleted = AuditLog.executeQuery("select ll.username from AuditLog ll where ll.event = ? ",[AuditLogEvent.ACCOUNT_DELETION])
    def actPurged = AuditLog.executeQuery("select ll.username from AuditLog ll where ll.event = ? ",[AuditLogEvent.ACCOUNT_PURGING])
    def list = AuditLog.executeQuery("""select sum(case when l.details like '%Gender: Male.%' and l.event = :actActivationEvent then 1 else 0 end),
            sum(case when  l.details like '%Gender: Female.%' and l.event = :actActivationEvent then 1 else 0 end),
            sum(case when  l.event = :actActivationEvent then 1 else 0 end),
            sum(case when  l.details like '%Gender: Male.%' and l.event = :actFinishRegEvent and ((l.username not in(:actActivation)) or (l.username in(:actDeleted) or l.username in(:actPurged))) then 1 else 0 end),
            sum(case when  l.details like '%Gender: Female.%' and l.event = :actFinishRegEvent and ((l.username not in(:actActivation)) or (l.username in(:actDeleted) or l.username in(:actPurged))) then 1 else 0 end),
            sum(case when  l.event = :actFinishRegEvent and ((l.username not in(:actActivation)) or (l.username in(:actDeleted) or l.username in(:actPurged))) then 1 else 0 end),
            sum(case when  l.details like '%Gender: Male.%' and l.event = :actDeletionEvent then 1 else 0 end),
            sum(case when  l.details like '%Gender: Female.%' and l.event = :actDeletionEvent then 1 else 0 end),
            sum(case when  l.event = :actDeletionEvent then 1 else 0 end),
            sum(case when  l.details like '%Gender: Male.%' and l.event = :actPurgedEvent then 1 else 0 end),
            sum(case when  l.details like '%Gender: Female.%' and l.event = :actPurgedEvent then 1 else 0 end),
            sum(case when  l.event = :actPurgedEvent then 1 else 0 end)                
        from AuditLog l
        where l.dateCreated >= :startDate and l.dateCreated < :endDate""",
        [
            actActivationEvent:AuditLogEvent.ACCOUNT_ACTIVATION,
            actDeletionEvent:AuditLogEvent.ACCOUNT_DELETION,
            actPurgedEvent:AuditLogEvent.ACCOUNT_PURGING,
            actFinishRegEvent:AuditLogEvent.FINISH_REGISTRATION,
            actActivation:actActivation,
            actDeleted:actDeleted,
            actPurged:actPurged, 
            startDate: startDate,
            endDate:endDate
        ])

这是我在数据库为空的情况下执行代码时的错误消息。

类:org.hibernate.hql.ast.QuerySyntaxException

消息:

子树的意外结束[选择总和(当 l.details 像 '%Gender: Male.%' 和 l.event = :actActivationEvent 然后 1 else 0 结束时的情况),sum(当 l.details 像 '%Gender 这样的情况: Female.%' and l.event = :actActivationEvent then 1 else 0 end), sum(l.event = :actActivationEvent then 1 else 0 end), sum(l.details like '%Gender: Male. %' and l.event = :actFinishRegEvent and ((l.username not in()) or (l.username in() or l.username in())) then 1 else 0 end), sum(case when l. '%Gender: Female.%' and l.event = :actFinishRegEvent and ((l.username not in()) or (l.username in() or l.username in())) then 1 else 0 end ), sum(当 l.event = :actFinishRegEvent and ((l.username not in()) or (l.username in() or l.username in())) then 1 else 0 end), sum(case当 l.details 像 '%Gender: Male.%' 和 l.event = :actDeletionEvent 然后 1 else 0 end), sum(当 l.details 像 '%Gender: Female.%' 和 l.event = :actDeletionEvent 时的情况然后 1 else 0 end), sum(当 l.event = :a 时的情况ctDeletionEvent then 1 else 0 end), sum(l.details like '%Gender: Male.%' and l.event = :actPurgedEvent then 1 else 0 end), sum(l.details like '%Gender: Female.%' and l.event = :actPurgedEvent then 1 else 0 end), sum(case when l.event = :actPurgedEvent then 1 else 0 end) from ph.gov.csc.comex.log.AuditLog l where l. dateCreated >= :startDate 和 l.dateCreated

我将如何解决这个问题?请帮忙!谢谢!

【问题讨论】:

    标签: grails hql


    【解决方案1】:

    问题在于您盲目地将前三个查询的结果作为参数传递给最后一个查询。这导致 where 子句包含:

    where ... l.username not in () ...
    

    这是无效的。 in () 子句必须在括号内至少有一个元素。如果您作为参数传递给in 子句的列表之一为空,您应该使用另一个查询。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-23
      • 2023-03-10
      • 1970-01-01
      • 2010-09-08
      相关资源
      最近更新 更多