我在Color
和Shade
之间有多对多关联。 Color
具有许多阴影,而Shades
具有许多颜色。
我这样建模:
class Color {
static hasMany = [shades: Shade]
String name
}
class Shade {
static belongsTo = Color
static hasMany = [colors: Color]
String name
}
但是,当我运行以下代码时:
new Color(name: "Red").addToShades(new Shade(name: "light")).save()
它仅将记录保存在
Color
表和Shade
表中,而不保存在Color_Shades
表中,后者实际上是两者之间的联接表。难道我做错了什么?那就是我从docs理解它的方式:
最佳答案
我不确定为什么不填充您的表,但是在this talk中建议Burt关于使用这种多对多类型的性能。解决方案是使用中间类:
class ColorShade implements Serializable {
Color color
Shade shade
//implement hashcode & equals!
//and also implement helpers like removeAll, remove, create and get.
static mapping = {
id composite: ['color','shade']
table 'Color_Shades'
version false
}
}
您可以在Spring Security Core plugin中看到一个示例类。
关于grails - Grails中的多对多未将记录保存在关系表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14941705/