我正在使用grails 2.4.2,并在我的controller-update方法中包含以下代码:
@Transactional
def update(ErtIncommingInvoice ertIncommingInvoiceInstance) {
if (ertIncommingInvoiceInstance == null) {
notFound()
return
}
// Concurrent-Update Test
if (ertIncommingInvoiceInstance.version != params.version as int) {
flash.warning = "Another user changed the record! (Concurrent Update Error)"
ertIncommingInvoiceInstance.errors.rejectValue("ertInfo", "concurrent.update.error")
respond ertIncommingInvoiceInstance.errors, view:'edit'
return
}
即使在这种情况下,也将检测到错误并设置了错误对象,并且方法流程未执行
ertIncommingInvoiceInstance.save flush:true, failOnError: true
数据已在数据库中更改。
显示编辑 View ,但不显示错误,仅显示闪烁消息。
我的推理错误在哪里?
最佳答案
Grails将在任何validate
之前调用save
并覆盖您在errors
对象中设置的任何内容。此外,方法完成后,Grails会自动在对象上调用save
。您应该在已更改的任何对象上调用discard()
,但不希望保留它们,或者使用withTransaction
块创建事务并手动回滚。
关于grails - grails:即使设置了错误也保存了数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38243568/