[英]many-to-many in grails is not saving records in the relationship table
我在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
表中,后者實際上是兩者之間的Color_Shades
表。
難道我做錯了什么? 那就是我從docs了解的方式:
我不確定為什么不填充您的表,但是在討論這種使用多對多類型的性能時,建議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插件中看到一個示例類。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.