我想从另一个“状态”基础上删除几个对象。我不断收到“删除的对象将被重新保存级联...”错误。Grails错误 - 删除对象将被重新保存
我已经搜索这个问题,阅读所有帖子,尝试了各种建议,但仍然无法使其工作。
我有两个互相引用的域。这是代码。希望有人能告诉我我做错了什么。而且,我是Grails的新人。如果它倾倒在我身上,所以我还在学习。
class Room {
static def hasMany = [devices : Device]
static def hasOne = [status: RoomStatus]
Integer roomId
String name
static constraints = {
roomId unique: true
}
static mapping = {
devices sort:'id', order: 'asc'
}
}
class Device {
static def belongsTo = [room: Room]
static def hasOne = [status: DeviceStatus]
Integer deviceId
String name
static constraints = {
deviceId nullable: true, unique: true
}
static mapping = {
}
}
下面是我用来从房间中删除“已删除”状态的所有设备的方法。它是在房间控制器:
def removeDeletedDevices(Long id) {
def roomInstance = Room.get(id)
if (!roomInstance) {
// redirect to error page
return
}
for (def device : roomInstance.devices) {
if (true == device.status.toString().equals("Deleted")) {
try {
device.delete(flush: true)
} catch (DataIntegrityViolationException e) {
// report error
break;
}
}
}
// redirect to report page.
}
我之前删除试图
- roomInstance.removeFromDevices(设备)。
- beforeDelete在设备控制器
至今没有运气。我究竟做错了什么?
可能重复[这里](http://stackoverflow.com/a/17412425/2051952)。 – dmahapatro