Can I use geCurrentSession() instead of using HibernateCallback to get the session?. If both sessions are managed by Spring, why do I need a Callback ? Is there any performance advantage associated with this ?
Can I use geCurrentSession() instead of using HibernateCallback to get the session?. If both
Share
In a well-configured Spring application, both are equivalent except in one case: when there is no current transaction context. In this case, getCurrentSession() won’t work, whereas using a HibernateCallback will open a session and a Hibernate transaction for you and close them at the end.
Note that the documentation of HibernateTemplate mentions that getCurrentSession() should be preferred:
It also says:
Mixing both ways of doing is thus not a good idea.
My preference is to avoid HibernateTemplate, use the Hibernate Session API directly, and use Spring-managed declarative transactions.