我有一个名为People的Grails域,我想检查每个People是否有 child 。子项是其他“人”对象。这是我的域结构:
class People implements Serializable {
static constraints = {
name (nullable : false, unique : true)
createdBy (nullable : false)
creationDate (nullable : false)
}
static transients = ['hasChild']
static mapping = {
table 'PEOPLE'
id generator: 'sequence', params : [sequence : 'SEQ_PK_ID']
columns {
id column : 'APEOPLE_ID'
parentPeople column : 'PARENT_PEOPLE_ID'
}
parentPeople lazy : false
}
People parentPeople
String name
String description
Boolean hasChild() {
def childPeoples = People.createCriteria().count {
eq ('parentPeople', People)
}
return (childPeoples > 0)
}
}
但是我无法在任何地方调用people.hasChild()。你能帮我这个忙吗?非常感谢!
最佳答案
这是因为在eq ('parentPeople', People)
中,Grails无法理解“人”是什么(这是一个类)。您应该用this
替换“People”。例如:
static transients = ["children"]
def getChildren() {
def childPeoples = People.findAllByParentPeople(this, [sort:'id',order:'asc'])
}