Grails GORM createCriteria();及;问题
2域类、个人和属性Grails GORM createCriteria();及;问题,grails,gorm,Grails,Gorm,2域类、个人和属性 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", "f
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
}
结果:
[酒吧
,富吧]
查询lname或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
}
结果:
[福福
,富吧
,bar]
但如果我改变或改变,我不会得到任何结果:
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
}
结果:
[]我发现了一个棘手的问题:
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)
我不回答这个问题,希望有人能想出更好的办法