Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transient property in Grails domain

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!

like image 284
Đinh Hồng Châu Avatar asked Mar 22 '11 08:03

Đinh Hồng Châu


1 Answers

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'])
    }
like image 168
Hoàng Long Avatar answered Sep 18 '22 23:09

Hoàng Long