gpt4 book ai didi

Grails 清除 hasMany 条目并添加新条目错误?

转载 作者:行者123 更新时间:2023-12-02 13:46:26 24 4
gpt4 key购买 nike

我目前正在处理 grails 应用程序,并且我有一个附加到帐户的地址列表。基本上我想要做的是在编辑帐户时显示所有附加地址的当前列表,然后我可以从 View 中删除/添加任意数量的地址。当这个数据被捕获时,它被 Controller 拾取,我想要做的是能够从这个帐户中清除所有当前地址,然后使用 View 中存在的内容再次创建列表,我的代码如下:

账户域:

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)

....

}

错误:

A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance: com.tool.Account.addresses. Stacktrace follows:
Message: A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance: com.tool.Account.addresses



此错误似乎发生在在线 Controller 中:
accountInstance.save(flush: true)

我已经尝试了几种不同的方法来让它工作,并且非常感谢一些帮助。

最佳答案

因此,您似乎已经完成了一些 Grails 可以为您完成的工作。

class Account {

String name
Date dateCreated
Date lastUpdated

List addresses

static hasMany = [addresses:Address]

static mapping = {
addresses cascade:"all-delete-orphan"
}

static constraints = {
name(blank:false, unique: true)
}

}

class Address {
String firstLine
String postcode
String area

static belongsTo = [account:Account]
}

这将产生您想要的地址作为列表的效果。

我找到了
instance.addresses = null

或者
instance.addresses.clear()

为我工作

关于Grails 清除 hasMany 条目并添加新条目错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18944042/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com