Thursday, September 1, 2011

Hibernate "not-found" in Grails

Often when you delete object from db instead of just marking it as deleted, there is nasty exception that this object is referenced by someone else.
2011-09-01 15:18:38,953 [http-8080-1] ERROR errors.GrailsExceptionResolver  - Exception occurred when processing request: [GET] xxx
Stacktrace follows:
org.codehaus.groovy.grails.web.pages.exceptions.GroovyPagesException: Error processing GroovyPageView: Error executing tag <g:form>: Error executing tag <sec:ifAllGranted>: Error evaluating expression [xxx.yyy?.id] on line [70]: No row with the given identifier exists: [xxx#21244] at xxx.gsp:71 at xxx.gsp:102
    at java.lang.Thread.run(Thread.java:662)
Caused by: org.codehaus.groovy.grails.web.taglib.exceptions.GrailsTagException: Error executing tag <g:form>: Error executing tag <sec:ifAllGranted>: Error evaluating expression [xxx.yyy?.id] on line [70]: No row with the given identifier exists: [yyy#21244] at xxx.gsp:71 at xxx.gsp:102
    ... 1 more
Caused by: org.codehaus.groovy.grails.web.taglib.exceptions.GrailsTagException: Error executing tag <sec:ifAllGranted>: Error evaluating expression [xxx.yyy?.id] on line [70]: No row with the given identifier exists: [xxx#21244] at xxx.gsp:71
    ... 4 more
Caused by: org.codehaus.groovy.grails.web.pages.exceptions.GroovyPagesException: Error evaluating expression [xxx.yyy?.id] on line [70]: No row with the given identifier exists: [xxx#21244]
    at grails.plugins.springsecurity.SecurityTagLib$_closure1.doCall(SecurityTagLib.groovy:60)
    ... 7 more
Caused by: org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [xxx#21244]
    ... 10 more
In Hibernate there is nice not-found="ignore" for such cases. In Grails/GORM there is similar option which is not documented for some reason. Just add ignoreNotFound to domain objects mapping section and that is it. For example:
class Log {
  User user

  static mapping = {
    user ignoreNotFound : true 
  }

}

No comments:

Post a Comment