在Grails中,我如何访问域类静态方法中的hibernate会话? [英] In Grails how do I access the hibernate session inside of a domain class static method?
问题描述
我已经阅读过网上的各种文章,但在这一点上他们似乎相当分散。在我的配置和我的方法中,我需要做些什么才能获得休眠会话。我试图做一些直接的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?
推荐答案
从静态方法中,您可以将sessionFactory bean来自应用程序上下文:
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
这篇关于在Grails中,我如何访问域类静态方法中的hibernate会话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!