我在 Controller 中有一个自定义验证,因为我需要使用数据库进行验证。
def update(Object instance) {
if (instance == null) {
notFound()
return
}
if (instance.hasErrors()) {
//redirect code here
return
}
def obj = objService.someMethod()
//some validation code here
if(someCheck){
// if i discard it wont work
instance.discard()
flash.error = message(code: 'message.code')
//render code here
return
}
在上面的代码中,即使执行数据库访问后,即使
instance.discard()
也无法使用。即使没有保存方法调用,更改的数据也会自动保存。如果不执行数据库访问,则相同的instance.discard()
将起作用。验证失败时,如何丢弃将更改后的值保存到数据库中。
最佳答案
在@Transactional(readonly = true)
操作上方添加注释update
。这应该够了吧。
这是Grails的内置功能,可自动将实例持久保存到数据库。
请在这里阅读我的答案以获取更多详细信息
https://stackoverflow.com/a/32659803/2405040
关于grails - GORM实例在更新问题上自动保留到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33896967/