【发布时间】:2013-09-22 12:55:36
【问题描述】:
我目前正在开发一个 grails 应用程序,并且我有一个附加到帐户的地址列表。基本上我想做的是在编辑帐户时显示所有附加地址的当前列表,然后我可以从视图中删除/添加任意数量的地址。捕获此数据后,控制器将获取该数据,我想要做的是能够清除此帐户中的所有当前地址,然后使用视图中存在的内容再次创建列表,我的代码如下:
帐户域:
class Account {
String name
Date dateCreated
Date lastUpdated
static hasMany = [addresses:Addresses]
static mapping = {
addresses cascade:"all-delete-orphan"
}
def getAddressesList() {
return LazyList.decorate(
addresses,
FactoryUtils.instantiateFactory(Addresses.class))
}
static constraints = {
name(blank:false, unique: true)
}
}
地址域:
class Addresses {
int indexVal
String firstLine
String postcode
String area
static belongsTo = [account:Account]
static mapping = {
}
static transients = [ 'deleted' ]
static constraints = {
indexVal(blank:false, min:0)
}
}
帐户管理员:
def update() {
def accountInstance = Account.get(params.id)
if (!accountInstance) {
flash.message = message(code: 'default.not.found.message', args: [message(code: 'account.label', default: 'Account'), params.id])
redirect(action: "list")
return
}
if (params.version) {
def version = params.version.toLong()
if (accountInstance.version > version) {
accountInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
[message(code: 'subscriptions.label', default: 'Subscriptions')] as Object[],
"Another user has updated this Account while you were editing")
render(view: "edit", model: [accountInstance: accountInstance])
return
}
}
accountInstance.properties = params
accountInstance.addresses.clear()
accountInstance.save(flush: true)
....
}
错误:
拥有的实体实例不再引用具有 cascade="all-delete-orphan" 的集合:com.tool.Account.addresses。堆栈跟踪如下:
消息:拥有的实体实例不再引用具有 cascade="all-delete-orphan" 的集合:com.tool.Account.addresses
这个错误好像是在线控制器出现的:
accountInstance.save(flush: true)
我已经尝试了几种不同的方法来让它工作,非常感谢一些帮助。
【问题讨论】:
-
不应该将
clear()设置为params之前的properties?交换这两行。 -
我已经尝试过了,但仍然得到同样的错误:S
-
如果我从 Accounts 域中删除行 (addresses cascade:"all-delete-orphan") 然后让控制器清除地址并添加新地址,我没有收到错误,但它只是将它们添加到已经创建一些重复项的列表中,有什么想法吗?
标签: grails grails-orm