以下 grails 脚本:
// Import.groovy
includeTargets << grailsScript("Bootstrap")
target(main: "Import some data...") {
depends(bootstrap)
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)
失败,但有以下例外:
执行脚本导入时出错:org.hibernate.HibernateException: No Hibernate Session bound to thread,并且配置不允许在此处创建非事务性会话
在无效域对象上调用 validate() 时。我想在验证失败时输出错误消息,但似乎我需要建立一个休眠会话才能这样做。任何人都知道克服这个问题的方法吗?