在 Grails 中,如何访问域类静态方法内的休眠会话?

发布于 2024-08-14 13:04:01 字数 150 浏览 3 评论 0原文

我在网上读过各种文章,但在这一点上它们似乎相当分散。我到底需要在我的配置和方法中做什么来获取休眠会话。我正在尝试对存储过程进行一些直接 sql 调用。我有一个从 Ruby 移植的大型代码库,其中包含大量静态方法和存储过程调用。如果我需要使用sessionFactory,那么如何访问它?

I've read various articles on the web, but they seem rather scattered on this point. Exactly what do I need to do in my configuration and in my method to get the hibernate session. I'm trying to make some direct sql calls for stored procedures. I have a large code base that I am porting from Ruby with lots of static methods and stored procedure calls. If I need to use the sessionFactory, then how to I get access to it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

痕至 2024-08-21 13:04:02

通过静态方法,您可以从应用程序上下文中提取 sessionFactory bean:

import org.codehaus.groovy.grails.commons.ApplicationHolder as AH
...
def ctx = AH.application.mainContext
def sessionFactory = ctx.sessionFactory
def session = sessionFactory.currentSession

From a static method you can pull the sessionFactory bean from the application context:

import org.codehaus.groovy.grails.commons.ApplicationHolder as AH
...
def ctx = AH.application.mainContext
def sessionFactory = ctx.sessionFactory
def session = sessionFactory.currentSession
站稳脚跟 2024-08-21 13:04:02

如果是在服务或者控制器中,只需要声明sessionFactory

定义会话工厂

将其注入。之后就可以参考

sessionFactory.currentSession

使用了。

查看 hibernate-filter 插件(文件 HibernateFilterGrailsPlugin.groovy)了解如何注入一堆使用会话的方法。

If it's in a service or controller, you just need to declare sessionFactory

def sessionFactory

to have it injected. After that you can refer to

sessionFactory.currentSession

to use it.

Check out hibernate-filter plugin (file HibernateFilterGrailsPlugin.groovy) for how to inject a bunch of methods that use the session.

夕色琉璃 2024-08-21 13:04:02

您可以使用withSession

Book.withSession { session ->
    session.clear()
}

更多信息

You can use withSession

Book.withSession { session ->
    session.clear()
}

More info

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文