Grails域中的瞬态属性 [英] Transient property in Grails domain

查看:154
本文介绍了Grails域中的瞬态属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为People的Grails域名,我想检查每个People是否有子。孩子是别人的对象。这是我的域结构:

  class People实现可序列化{

static constraints = {
name(nullable:false,unique:true)
createdBy(nullable:false)
creationDate(nullable:false)
}

static transients = ['hasChild ']

static mapping = {
table'PEOPLE'
id生成器:​​'sequence',params:[sequence:'SEQ_PK_ID']
columns {
id列:'APEOPLE_ID'
parentPeople列:'PARENT_PEOPLE_ID'
}
parentPeople lazy:false
}

人员parentPeople
字符串名称
字符串描述

Boolean hasChild(){
def childPeoples = People.createCriteria()。count {
eq('parentPeople',People)
}
return(childPeoples> 0)
}
}

但是我无法在任何地方调用people.hasChild()。你能请我帮忙吗非常感谢!

解决方案

这是因为在 eq('parentPeople',People),Grails不明白人是什么(这是一个类)。你应该用这个替换People。例如:

  static transients = [children] 

def getChildren(){
def childPeoples = People.findAllByParentPeople(this,[sort:'id',order:'asc'])
}


I have a Grails domain called People, and I want to check that each People has childs or not. Childs are other People objects. Here is my domain structure:

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)
    }
}

But I cannot call people.hasChild() at anywhere. Could you please helpe me on this? Thank you so much!

解决方案

It's because in eq ('parentPeople', People), Grails can't understand what "People" is (it's a class). You should replace "People" by this. For example:

static transients = ["children"]

    def getChildren() {
        def childPeoples = People.findAllByParentPeople(this, [sort:'id',order:'asc'])
    }

这篇关于Grails域中的瞬态属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆