2012-07-11 49 views
0

2域類,人與屬性GORM個createCriteria() 「和」 問題

class Person { 
static hasMany = [attributes : Attribute]  


} 


class Attribute{ 
String key 
String value 
} 

查詢FNAME:

def c = Person.createCriteria() 
def result = c.list{ 

attributes { 
      eq("key", "fname") 
      eq("value", "foo") 
} 


println result 
} 

結果: [富富 ,富巴]

查詢lname:

def c = Person.createCriteria() 
def result = c.list{ 

attributes { 
      eq("key", "lname") 
      eq("value", "bar") 
} 


println result 
} 

結果: [巴巴 ,FOO巴]

查詢L-NAME OR FNAME:

def c = Person.createCriteria() 
def result = c.list{ 
or{ 
attributes { 
      eq("key", "fname") 
      eq("value", "foo") 
} 

attributes { 
      eq("key", "lname") 
      eq("value", "bar") 
} 
} 

println result 
} 

結果: [富FOO ,FOO酒吧 ,酒吧酒吧]

但如果我改變或並且,我沒有得到任何結果:

def c = Person.createCriteria() 
def result = c.list{ 
and{ 
attributes { 
      eq("key", "fname") 
      eq("value", "foo") 
} 

attributes { 
      eq("key", "lname") 
      eq("value", "bar") 
} 
} 

println result 
} 

結果: []

回答

0

我發現了一個骯髒的工作,圍繞此:

def list1= Person.createCriteria().list { 
      attributes { 
        eq("key", "fname") 
        eq("value", "foo") 
      } 
     } 
def list2= Person.createCriteria().list { 
      attributes { 
        eq("key", "lname") 
        eq("value", "bar") 
      } 
     } 
     return list1.intersect(list2) 

Source

我將離開這個問題的答覆,希望有人會拿出更好的東西