我有一个修改和实例然后链接到另一个动作的动作。 如果实例上有错误,我想忽略我所做的更改。 但是,discard()方法不起作用。 状态更改始终保持不变。 我究竟做错了什么?
def reject={ def notice=Notice.get(params['id']); if(!notice){ flash.message="Could Not Find Notice With ID:"+params.id; redirect(action:'list'); return; } flash.messages=[:]; flash.errors=[:]; notice.status=NoticeType.REJECTED; if(!notice.hasErrors() && notice.save(flush:true)){ //success message flash.messages.notice_tab=["Notice Was Successfully Flagged As Rejected"]; } } else{ //error message flash.errors.notice_tab=[]; notice.errors.allErrors.each{ flash.errors.notice_tab.push(it); } notice.discard(); //THIS DOES NOTHING???? } chain(action:'edit', id:params['id'] ) return; }我也试过notice.refresh()将值设置回原始值。 没关系。 仍然坚持我设定的新价值观。 我将链改为重定向,同样的事情发生了。
I have an action which modifies and instance and then chains to another action. If there are errors on the instance I want to ignore changes I have made. However, the discard() method doesn't work. The status change is always persisted. What am I doing wrong?
def reject={ def notice=Notice.get(params['id']); if(!notice){ flash.message="Could Not Find Notice With ID:"+params.id; redirect(action:'list'); return; } flash.messages=[:]; flash.errors=[:]; notice.status=NoticeType.REJECTED; if(!notice.hasErrors() && notice.save(flush:true)){ //success message flash.messages.notice_tab=["Notice Was Successfully Flagged As Rejected"]; } } else{ //error message flash.errors.notice_tab=[]; notice.errors.allErrors.each{ flash.errors.notice_tab.push(it); } notice.discard(); //THIS DOES NOTHING???? } chain(action:'edit', id:params['id'] ) return; }I also tried notice.refresh() to set values back to original. Didn't matter. Still persisted the new values I set. I changed the chain to a redirect and the same things happened.
最满意答案
一种解决方案是使用命令对象而不是域对象。 然后,在命令对象上成功完成验证后,您可以将值复制到域对象,然后保存以避免Hibernate丢弃。
One solution would be to use a Command Object instead of a Domain Object. Then once validation is done successfully on your Command Object you can copy the values to the Domain Object and then save to avoid the Hibernate discard.
更多推荐
发布评论