Code:
public class GroovyHibernateTemplate extends HibernateTemplate {
public Object execute(boolean exposeNativeSession,final Closure worker){
return this.execute(new HibernateCallback() {
public Object doInHibernate(org.hibernate.Session session) throws HibernateException, SQLException {
return worker.call(session);
}
}, exposeNativeSession);
}
public Object execute(final Closure worker) {
return this.execute(new HibernateCallback() {
public Object doInHibernate(org.hibernate.Session session) throws HibernateException, SQLException {
return worker.call(session);
}
});
}
public List executeFind(final Closure worker) {
return this.executeFind(new HibernateCallback() {
public Object doInHibernate(org.hibernate.Session session) throws HibernateException, SQLException {
return worker.call(session);
}
});
}
}
then use it in Groovy like this:
Code:
template.execute{ session->
def query = session.createQuery("select count(*) from TestObject");
return query.iterate().next();
}