一个简单的博客应用程序,Grails 1.3.9 和 MySQL,两个域类之间的多对多关系,BlogPost 和 Tag
class BlogPost {
String title
String teaser
String body
Date updated
Category category
Integer priority
static hasMany = [comments:Comment,tags:Tag]
static belongsTo = [Category,Tag]
static searchable = true
String toString() {
"$title"
}
static constraints = {
title(nullable:false,blank:false,lenght:1..50)
teaser(nullable:false,blank:false,lenght:1..100)
body(nullable:false,blank:false,maxSize:5000)
updated(nullable:false)
category(nullable:false)
priority(nullable:false)
}
}
class Tag {
String name
String description
static hasMany = [blogpost:BlogPost]
static searchable = true
String toString() {
"$name"
}
static constraints = {
name(nullable:false,blank:false)
description(nullable:false,blank:false)
}
}
Hibernate 在 MySQL 中创建三个相关的表:blog_post、tag 和 tag_blogpost
现在,如果我创建与博客文章 Y 相关的标签 X,然后删除 Y,则在 tag_blogpost 表中保留一个孤立行,标签 X 的显示视图抛出异常“不存在具有给定标识符的行:[...] "
如何在 tag_blogpost 表中自动删除(级联)孤立行?