gpt4 book ai didi

grails - 为什么 Grails 在返回后将我的对象保存到数据库

转载 作者:行者123 更新时间:2023-12-02 15:17:03 24 4
gpt4 key购买 nike

我正在使用 Grails 2.4.5 版本。我在保存时检查条件。如果我的条件匹配,我会尝试返回一条消息而不是保存。但它向我显示了我想要的消息,但同时它将对象属性保存到数据库。
这是我的代码:

@Transactional
def save(AccountHead accountHeadInstance) {
if (!request.method.equals('POST')) {
redirect(action: 'index')
return
}
LinkedHashMap result = new LinkedHashMap()
String outPut

if (accountHeadInstance.id) {
if (accountHeadInstance.id == accountHeadInstance.subParentId) {
result.put(CommonUtils.IS_ERROR, Boolean.TRUE)
result.put(CommonUtils.MESSAGE, "Sorry, Same account can not be parent of it's own")
outPut = result as JSON
render outPut
return
// here should it return, is shows the message ok but saving the object. if I print something after this block it does not execute
}
}

if (accountHeadInstance.hasErrors()) {
def errorList = accountHeadInstance?.errors?.allErrors?.collect{messageSource.getMessage(it,null)}
result.put(CommonUtils.IS_ERROR, Boolean.TRUE)
result.put(CommonUtils.MESSAGE, errorList?.join('\n'))
outPut = result as JSON
render outPut
return
}

accountHeadInstance.hasChild = params.position == CommonUtils.POSITION_FIRST
accountHeadInstance.save flush:true
result.put(CommonUtils.MESSAGE, "Account head saved successfully.")
outPut = result as JSON
render outPut
return
}

最佳答案

好吧,您正在使用@Transactional,因此对域对象的任何更改都会将该对象设置为脏对象,并且当事务结束时,它们将被隐式保存。为了防止这种情况,您可以使用 accountHeadInstance.discard()。

但是,我不得不告诉您,您的代码是多个 Grails 反模式。

任何业务逻辑,尤其是使用 @Transactional 的业务逻辑都应该移到服务中,而不是在 Controller 中完成。 Controller 应该只用于绑定(bind)传入参数、路由到服务和渲染结果。

虽然您可以将域用作命令对象绑定(bind)参数,但从安全角度来看,这通常不是一个好主意。

或者,您可以执行以下“Groovy”操作:

LinkedHashMap result = [:]
result[CommonUtils.IS_ERROR] = Boolean.TRUE)
render result as JSON

关于grails - 为什么 Grails 在返回后将我的对象保存到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54493767/

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