我目前正在开发一个grails项目,我需要执行一个hibernate查询。每次我用我的数据库中的现有数据运行我的代码,一切正常。但是,如果我在数据库为空的情况下运行它,则会出现错误。
这是我的代码:
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的情况,如'%性别:男。%'和l.event =:actActivationEvent然后1其他0结束),总和(l.details像'%性别的情况:女。%'和l.event =:actActivationEvent然后1其他0结束),sum(l.event =:actActivationEvent然后1其他0结束时的情况),sum(l.details的情况,如'%Gender:Male。 %'和l.event =:actFinishRegEvent和((l.username不在())或(l.username in()或l.username in()))然后1 else 0 end),sum(l时的情况)。详细信息如'%Gender:Female。%'和l.event =:actFinishRegEvent和((l.username不在())或(l.username in()或l.username in()))然后1 else 0 end ),sum(l.event =:actFinishRegEvent和((l.username不在())或(l.username in()或l.username in())中的情况)然后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然后1 else 0 end),sum(l.details的情况,如'%性别:男。%'和l.event =:actPurgedEvent然后1其他0结束),总和(l.details像'%性别:女性。%'和l.event =:actPurgedEvent然后1其他0结束),总和(l.event =:actPurgedEvent然后1其他0结束时的情况)来自ph.gov.csc.comex.log.AuditLog l其中l。 dateCreated&gt; =:startDate和l.dateCreated&lt; :结束日期]
我将如何解决这个问题?请帮忙!谢谢!
答案 0 :(得分:2)
问题来自于您将三个第一个查询的结果作为参数盲目地传递给最后一个查询。这导致where子句包含:
where ... l.username not in () ...
无效。 in ()
子句必须至少在括号内有一个元素。如果您作为in
子句的参数传递的列表之一为空,则应使用另一个查询。