Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Grails how do I access the hibernate session inside of a domain class static method?

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?

like image 970
Andrew Avatar asked Dec 14 '09 02:12

Andrew


2 Answers

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
like image 148
Burt Beckwith Avatar answered Sep 28 '22 05:09

Burt Beckwith


You can use withSession

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

More info

like image 30
Michal Z m u d a Avatar answered Sep 28 '22 04:09

Michal Z m u d a