Spring Object Relational Mapping (ORM) package for Hibernate
Good practice for Transaction management.
All request are handled by separate thread so the transaction manager helps to bind Transaction with the current thread called thread-bound. When Using ORM we use Session for handling database resource when we use transaction manager using the Session is known as thread-bound Session.
Handling DB Transaction in Service layer gives better reusable of DAO Layer also Spring Frameworks recommends this approach.
Using declarative transaction management gives flexible for least impact on application code
Spring Data access methods
This is another direct alternative way to working with the raw Hibernate3 Session API which provides
The session can be created by either one of the following
HibernateTemplate hibernateTemplate = new HibernateTemplate(sessionFactory); or Session session = getHibernateTemplate().getSessionFactory().openSession();
1. Hibernate Template is thread safe and reusable.
2. You need not manually open and close Session.
3. Automatic conversion from HibernateExceptions to DataAccessExceptions.
The LocalSessionFactoryBean reference is preferred for creating HibernateTemplate.
The datasource either by DriverManagerDataSource or JndiObjectFactoryBean reference for LocalSessionFactoryBean.
The abstract class holds the HibernateTemplate property but also provide working with a Hibernate Session directly.
Hence more flexible for extending for any DAO classes
The session can be created by the following
Session session = this.getSession();
AOP proxy is used for Hibernate session management.
This interceptor binds a new Hibernate Session with TransactionSynchronizationManager to the thread before a method call, closing and removing it afterwards in case of any method outcome.
This interceptor automatically translates HibernateExceptions via delegating to the SessionFactoryUtils.convertHibernateAccessException
When to use :
Since interceptor binds Hibernate Session with TransactionSynchronizationManager use it when ever the data access code needs to execute with in transactions.
Avoid using intereceptor when data are not transaction dependent.
The Existing session can be created by either one of the following
Session session = SessionFactoryUtils.getSession() or Session session = SessionFactory.getCurrentSession()