在删除中捕获(数据完整性违规异常)错误后更改域的属性。圣杯 2.5



在删除中发现错误后,我正在尝试修改域属性。我的操作代码如下:

@Transactional
def delete(User userInstance) {
    if (userInstance == null) {
        notFound()
        return
    }
    try {
        userInstance.delete(flush: true)
    }
    catch(e) {
        userInstance.active = false
        userInstance.save(flush: true)
        render status: 200
        return
    }

    request.withFormat {
        form multipartForm {
            flash.message = message(code: 'default.deleted.message', args: [message(code: 'user.label', default: '${className}'), userInstance.id])
            redirect action:"index", method:"GET"
        }
        '*'{ render status: NO_CONTENT }
    }
}

如果在删除时发生错误,我希望将活动属性值更改为false。

您获得DataIntegrityViolationException是因为您违反了一些外键关系规则,在您的情况下,您的user实例很可能被其他域对象(表)引用。

userInstance.active = false正常的情况下,您可以在catch块上执行任何您想要的操作,但您不会因此而得到错误(再次从catch块中引发另一个错误)。

最新更新