额外的属性使Grails将关系解释为双向多对一关系

时间:2014-08-07 12:38:11

标签: grails gorm

我真正的课程是不同的,所以请耐心等待我对这个例子的有限认识。 假设我有两个类:树和工具。树有许多授权工具。但是其中一个工具是一个连接两棵树的绳子,所以Rope扩展了Tool并且有一个属性endTree来存储另一棵树:

class Tree { 
    static hasMany = [tools: Tool]
}

class Tool {
    // it doesn't matter here what the tool can do
}

class Rope extends Tool {
    Tree endTree
}

我遇到的问题是Grails认为这种关系是双向的,所以在评估实体时,AbstractGrailsDomainBinder中的isBidirectionalManyToOneWithListMapping()返回true,实际上将endTree属性设置为updateable = false,hibernate在保存期间跳过endTree更新

我做错了什么?

谢谢,

MB

1 个答案:

答案 0 :(得分:2)

您可以明确告诉Grails 使用mappedBy值为“none”将属性视为双向关系的一方:

class Tree { 
    static hasMany = [tools: Tool]
    static mappedBy = [tools: "none"]
}

和/或Rope端的相同内容:

class Rope extends Tool {
    Tree endTree
    static mappedBy = [endTree: "none"]
}

我不确定你是否需要两端的mappedBy或者其中一个是否足够。

相关问题