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", "foo")
}
println result
}结果: foo foo,foo bar
查询lname:
def c = Person.createCriteria()
def result = c.list{
attributes {
eq("key", "lname")
eq("value", "bar")
}
println result
}结果: bar bar,foo bar
查询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
}结果: foo foo,foo bar,bar bar
但如果我将OR更改为And,则不会得到任何结果:
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
}结果:[]
发布于 2012-07-12 05:39:27
我找到了一个解决这个问题的方法:
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
这个问题我就不回答了,希望有人能想出更好的办法
https://stackoverflow.com/questions/11441086
复制相似问题