2012-06-26 150 views
0

我目前正在開發一個grails項目,我需要執行一個hibernate查詢。每次我運行我的代碼與我的數據庫上的現有數據,一切工作正常。但是如果我用空的數據庫運行它,我得到了一個錯誤。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

消息:

子樹[SELECT SUM(案件意外結束時l.details LIKE '%性別:男%' 和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 I.event =:actActivationEvent then 1 else 0 end),sum(當l.details如'%Gender:Male。%'和l.event =:actFinishRegEvent和((l.username not in())或(l.username in ()或l.username in()))then 1 else 0 end),sum(case when l.details like'%Gender:Female。%'和l.event =:actFinishRegEvent和((l.username not in())或(l.username in()or l.username in())then 1 else 0 end),sum(case when l.event = :actFinishRegEvent和((l.username not in())or(l.username in()or l.username in())then 1 else 0 end),sum(case when l.details like'%Gender:Male 。%'和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當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 when。來自ph.gov.csc.comex.log的'%Gender:Female。%'和l.event =:actPurgedEvent then 1 else 0 end),sum(case when when.event =:actPurgedEvent then 1 else 0 end) .AuditLog l其中l.dateCreated> =:startDate和l.dateCreated <:endDate]

我將如何解決這個問題?請幫助!謝謝!

回答

2

問題來自您將三個第一查詢的結果盲目地作爲參數傳遞給最後一個查詢的事實。這導致where子句包含:

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

這是無效的。一個in()子句必須在括號內至少有一個元素。如果您作爲in子句的參數傳遞的列表中的一個爲空,則應該使用另一個查詢。

+0

非常感謝你!這真的有幫助! – chemilleX3