【问题标题】:Why Grails is saving my object to database after returning为什么 Grails 在返回后将我的对象保存到数据库
【发布时间】:2019-02-02 14:00:57
【问题描述】:

我使用的是 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
}

【问题讨论】:

    标签: grails grails-2.0


    【解决方案1】:

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

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

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

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

    您可以选择像这样做“Groovy”的事情:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-26
      • 1970-01-01
      • 1970-01-01
      • 2022-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-04
      相关资源
      最近更新 更多