Tuesday, February 21, 2012

Grails: Fails to save from within afterInsert closure

When trying to save a new domain object from within an afterInsert event, an exception was thrown using the following code:

    def afterInsert = {
        ADomainClass domainObject = new ADomainClass()
        domainObject.save() // <------------- THIS LINE FAILS
    }

When I modified the code slightly, then it worked as follows:


    def afterInsert = {
        ADomainClass.withNewSession {
           ADomainClass domainObject = new ADomainClass()
           domainObject.save() //
        }
    }

Reference: http://jira.grails.org/browse/GRAILS-4851

No comments:

Post a Comment