gpt4 book ai didi

hibernate - 如何在 grails 脚本中建立 Hibernate session

转载 作者:行者123 更新时间:2023-12-01 03:01:10 25 4
gpt4 key购买 nike

以下 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)

失败,但异常:

Error executing script Import: org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here



当在无效域对象上调用 validate() 时。我想在验证失败时输出错误消息,但似乎我需要建立一个 hibernate session 才能这样做。任何人都知道一种方法来克服这个?

最佳答案

找到 RunScript.groovy它为您设置 hibernate session ,然后运行您指定为参数的脚本。我不得不将我的源代码从 Gant 脚本(上图)改为:

// Import.groovy

def Channel = grailsApp.classLoader.loadClass("content.Channel")

def c
c = Channel.newInstance(title:"A Channel", slug:"a-channel", position:0).validate()
c = Channel.newInstance().validate()
println(c.errors)

我可以像这样运行它:

$> grails run-script scripts/Import.groovy

关于hibernate - 如何在 grails 脚本中建立 Hibernate session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/675514/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com