【发布时间】:2015-04-27 01:42:52
【问题描述】:
给定以下域类:
class Location {
String city
static belongsTo = [ author: Author ]
}
class Author {
String name
Location location
}
当我这样做时:
def l = new Location(city: "Boston")
l.save()
def a = new Author(name: "Niall Ferguson", location: l)
a.save()
我可以使用a.delete() 删除我的作者,但如果我尝试使用l.delete() 删除我的位置,我会收到此错误:
org.hibernate.ObjectDeletedException: deleted object would be re-saved by cascade (remove deleted object from associations): [Location#17]
好的,让我从关联中删除要删除的对象,然后重试:
author.location = null // Assuming these two statements are the right way
location.author = null // to to this.
现在当我再次尝试l.delete() 时,我得到了这个:
ERROR util.JDBCExceptionReporter - Referential integrity constraint violation: "FKAC2D218B9615BDBA: PUBLIC.AUTHOR FOREIGN KEY(LOCATION_ID) REFERENCES PUBLIC.LOCATION(ID) (18)";
那我做错了什么?在这种情况下甚至可以删除位置并保留作者吗?
【问题讨论】:
-
使用
static belongsTo = [ author: Author ]配置,您将获得gorm docs 中解释的行为(示例b)。我假设通过明确设置belongsTo 配置,您想说没有作者就不能存在位置。我建议删除 belongsTo 关联,这样就可以在不删除引用作者条目的情况下删除位置。 -
@MarioDavid 删除belongsTo后,我仍然不能只删除该位置,我收到我在问题中发布的第二个错误,完整性约束违规。
标签: grails