首页 > 解决方案 > 为什么 Grails 在返回后将我的对象保存到数据库

问题描述

我正在使用 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
}

标签: grailsgrails-2.0

解决方案


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

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

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

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

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

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

推荐阅读