I am doing something like this and it works for me ...
// Import.groovy includeTargets << grailsScript("Bootstrap") target(main: "Import some data...") { depends(bootstrap) // added this ------------------------------------------------------ def sessionFactory = appCtx.getBean("sessionFactory") def session = SessionFactoryUtils.getSession(sessionFactory, true) TransactionSynchronizationManager.bindResource( sessionFactory, new SessionHolder(session)) // added this ------------------------------------------------------ def Channel = grailsApp.classLoader.loadClass("content.Channel") def c // works: saving a valid Channel succeeds c = Channel.newInstance(title:"A Channel", slug:"a-channel", position:0).validate() // doesn't work: saving an invalid Channel fails with exception c = Channel.newInstance().validate() // this line is never reached due to exception println(c.errors) } setDefaultTarget(main)
source share