Grails Transient具有许多属性和自动魔法设置器
我创建了一个域类,它使用一个瞬态属性来代替真正的外键关系Grails Transient具有许多属性和自动魔法设置器,grails,gorm,grails-2.0,Grails,Gorm,Grails 2.0,我创建了一个域类,它使用一个瞬态属性来代替真正的外键关系 //ChildThing.groovy class ChildThing { String name static constraints = { } static mapping = { datasource("two") //table name: "child_thing" version false } } //ParentThing.gro
//ChildThing.groovy
class ChildThing {
String name
static constraints = {
}
static mapping = {
datasource("two")
//table name: "child_thing"
version false
}
}
//ParentThing.groovy
class ParentThing {
String name
static transients = [
'children'
]
static hasMany = [children: ChildThing]
Set<ChildThing> getChildren() {
ParentThingChildThing.findAllByParentThing(this).collect { ChildThing.get(it.childThing) } as Set
}
def setChildren(List<Long> children) {
children.each {
ParentThingChildThing.findOrSaveWhere([parentThing: this, childThing: it])
}
}
}
//ParentThingChildThing.groovy
import org.apache.commons.lang.builder.HashCodeBuilder
class ParentThingChildThing implements Serializable {
ParentThing parentThing
Long childThing
boolean equals(other) {
if (!(other instanceof ParentThingChildThing)) {
return false
}
other.parentThing?.id == parentThing?.id &&
other.childThing?.id == childThing
}
int hashCode() {
def builder = new HashCodeBuilder()
if (parentThing) builder.append(parentThing.id)
if (childThing) builder.append(childThing)
builder.toHashCode()
}
static mapping = {
id composite: ['parentThing', 'childThing']
version false
}
}
//ChildThing.groovy
班级儿童事务{
字符串名
静态约束={
}
静态映射={
数据源(“两个”)
//表名:“child_thing”
版本错误
}
}
//太棒了
阶级亲子关系{
字符串名
静态瞬变=[
“儿童”
]
static hasMany=[children:ChildThing]
Set getChildren(){
ParentThingChildThing.findAllByParentThing(this).collection{ChildThing.get(it.ChildThing)}作为集合
}
def setChildren(列出子项){
每个孩子{
ParentThingChildThing.Findorsavehere([parentThing:this,childThing:it])
}
}
}
//ParentThingChildThing.groovy
导入org.apache.commons.lang.builder.HashCodeBuilder
类ParentThingChildThing实现可序列化{
亲子关系亲子关系
长子
布尔等于(其他){
if(!(ParentThingChildThing的其他实例)){
返回错误
}
其他.parentThing?.id==parentThing?.id&&
其他.childThing?.id==childThing
}
int hashCode(){
def builder=新的HashCodeBuilder()
if(parentThing)builder.append(parentThing.id)
if(childThing)builder.append(childThing)
builder.toHashCode()
}
静态映射={
id组合:['parentThing','childThing']
版本错误
}
}
我希望通过一个命名的参数传递子对象
,并设置parentThing.properties=params
除了那一个目标外,我可以让一切顺利。是否可以将自动魔法属性重写为参数设置器?您是在谈论重写绑定的工作方式,还是重写类的getter和setter?您是否声明了def setChildred(v){…}方法?@FabianoTaioli是的,我声明过,但是没有成功。您能发布完整的类代码吗?尝试用'def setChildren(children){'更改setChildren的定义