Override getter and setter in grails domain class for relation

前提是你 提交于 2019-12-19 06:45:35

问题


How to override getter and setter for field being a relation one-to-many in grails domain class? I know how to override getters and setters for fields being an single Object, but I have problem with Collections. Here is my case:

I have Entity domain class, which has many titles. Now I would like to override getter for titles to get only titles with flag isActive equals true. I've tried something like that but it's not working:

class Entity {

    static hasMany = [
        titles: Title
    ]

    public Set<Title> getTitles() {
        if(titles == null)
            return null
        return titles.findAll { r -> r.isActive == true }
    }

    public void setTitles(Set<Title> s) {
        titles = s
    }
}

class Title {
    Boolean isActive

    static belongsTo = [entity:Entity]

    static mapping = {
        isActive column: 'is_active'
        isActive type: 'yes_no'
    }
}

Thank You for your help.


回答1:


Need the reference Set<Title> titles.

class Entity {
    Set<Title> titles

    static hasMany = [
        titles: Title
    ]

    public Set<Title> getTitles() {
        if(titles == null)
            return null;
        return titles.findAll { r -> r.isActive == true }
    }

    public void setTitles(Set<Title> s) {
        titles = s
    }
}


来源:https://stackoverflow.com/questions/17169657/override-getter-and-setter-in-grails-domain-class-for-relation

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!