我正在使用 Grails 2.3.4。我正在尝试检索具有特定条件的域对象,但是当属性为空时我无法解决。这是一个例子。
class Domain1 {
int locationId
Domain2 domain2
Domain3 domain3
...
static constraints = {
locationId(nullable:false, blank:false, unique:false)
domain2(nullable:true)
domain3(nullable:true)
...
}
}
class Domain2 {
int locationId
...
static constraints = {
locationId(nullable:false, blank:false, unique:false)
...
}
}
class Domain3 {
int locationId
...
static constraints = {
locationId(nullable:false, blank:false, unique:false)
...
}
}
如果 locationId 在 Domain1 中有效,则该查询仅假设返回单个 Domain1,如果 locationId 不为 null,则 locationId 在 Domain2 中有效,如果 locationId 不为 null,则 locationId 在 Domain3 中有效。
def getDomain1ById(Long sid) {
return Domain1.createCriteria().get {
idEq(sid)
'in' ("locationId", Util.getUserAccessLocationList())
// What I want to do is if(domain2 != null) check location
or {
isNull("domain2")
domain2 {
'in' ("locationId", Util.getUserAccessLocationList())
}
}
// What I want to do is if(domain3 != null) check location
or {
isNull("domain3")
domain3 {
'in' ("locationId", Util.getUserAccessLocationList())
}
}
}
}
我错过了什么。如果 domain2 和 domain3 不为空,则查询工作正常