2

我创建了一个域类,它使用瞬态属性代替真正的外键关系。

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

我希望children通过一个命名参数并设置parentThing.properties = params

除了一个目标,我可以让一切工作。是否可以将自动魔术属性覆盖到参数设置器?

4

0 回答 0