2012-04-03 46 views
0

試圖想出一個簡潔的方式來使用NOT IN子句創建命名查詢。限制類似乎沒有「不在」構造。任何人都知道這樣做的好方法?針對NOT IN的Grails命名查詢

解決方案

static namedQueries = { 
     activeOnly { 
      eq 'active', true 
     } 
     open { 
      ne 'state', this.STATE_COMPLETED 
     } 
     my { user -> 
      or { 
       eq 'createdBy', user 
       eq 'reviewer', user 
       eq 'assignee', user 
       eq 'requestedFor', user 
       ilike 'notifyList', "%$user%" 
      } 
     } 
     topLevel { 
      not {'in'('type', [RequestType.Part])} 
     } 
    } 

注意:單詞「in'must被引用,因爲它保留。

這些命名查詢可以使用這樣的:

Request.activeOnly.open.topLevel.list(params) 

回答

1

也許sombething這樣的:

Book.findAll("from Book b where b.author not in (?)", ['Borges', 'Sabato', 'Cortazar']) 

或使用標準

http://grails.org/doc/1.3.x/ref/Domain%20Classes/createCriteria.html - 在文檔中的「 '標準方法也有一個鏈接'不'的方法。

+0

謝謝你的回覆。這可能工作,但不是一個命名查詢。命名查詢使用標準API來創建和緩存預準備語句。 – dbrin 2012-04-03 23:23:46

+0

添加標準dsl。 – pablomolnar 2012-04-03 23:41:16

+0

謝謝你的時間。更正了語法。 inList是爲約束保留的,但引用的'in'起作用。 – dbrin 2012-04-03 23:57:12