user2782001
user2782001

Reputation: 3488

grails hiberate discard() being ignored

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.

Upvotes: 0

Views: 470

Answers (1)

Joe
Joe

Reputation: 1219

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.

Upvotes: 1

Related Questions