本文介绍了覆盖grails域类中的getter和setter以获取关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 限时删除!! 如何重写getter和setter作为grails领域类中的一对多关系的字段?我知道如何覆盖作为单个对象的字段的getters和setter,但我对Collections有问题。这是我的情况: 我有Entity域类,它有很多标题。现在我想重写标题的getter以获得只有标志isActive等于true的标题。我试过类似的东西,但它不起作用: class实体{ static hasMany = [标题:标题] 公共设置<标题> getTitles(){ if(titles == null) return null return titles.findAll {r - > r.isActive == true} } public void setTitles(Set< Title> s){ titles = s } } 类标题{布尔isActive 静态belongsTo = [实体:实体] 静态映射= { isActive列: 'is_active' isActive type:'yes_no'} } 解决方案需要参考 Set< Title> title 。 class实体{ Set< Title> title 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 } } 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. 解决方案 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 }} 这篇关于覆盖grails域类中的getter和setter以获取关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 1403页,肝出来的.. 09-08 23:23