The Hibernate SessionFactory Example shows you how to use SessionFactory in Hibernate. Hibernate SessionFactory is used to create Sessions. Each application is having usually only one SessionFactory. Hibernate SessionFactory is thread safe, so that many threads can access it concurrently and requests the sessions. Hibernate SessionFactory provides three methods through which we can get Session object – getCurrentSession(), openSession() and openStatelessSession().
Other interesting posts you may like
- Hibernate Hello World Example in Eclipse (Xml configuration)
- Hibernate Annotations Example in Eclipse
- Hibernate One-To-One Mapping Example using Annotation (Shared Primary Key)
- Hibernate One-To-One Mapping Example using Foreign key Annotation
- Hibernate Many to One Mapping Annotation Example
- Hibernate Many to One Bidirectional Mapping Annotation Example
- Hibernate Many-To-Many Unidirectional Mapping Example
- Hibernate Many-To-Many Bidirectional Mapping Example
- Hibernate First Level Cache Example
- Hibernate Second Level Cache Example - Ehcache configuration example
- Difference between get and load method in Hibernate
- Difference between save and saveOrUpdate in Hibernate
- Difference between save and persist method in Hibernate
- Constructor based HQL - Select new object in Hibernate
- Hibernate Query Cache Example
Hibernate SessionFactory getCurrentSession
The getCurrentSession() method returns the session bound to the context. To use this method we have to configure it in hibernate configuration file like this:
But for this to work, we need to configure it in hibernate configuration file like below.
If the session is not bound to the thread context, the we will get the exception like below
Exception in thread "main" org.hibernate.HibernateException: No CurrentSessionContext configured!
Thank to the session object belongs to the thread context so that this session will be closed automatically once the session factory is closed.
Notice that Hibernate Session objects are not thread safe, so it is not used in multi-threaded environment. It is the best choice to use in single threaded environment because it’s faster than opening a new session.
Hibernate SessionFactory openSession
The openSession() method always opens a new session. This session object should be closed when we are done with all the CRUD operations.
One more thing, We should call the openSession() method to open a new session for each request in multi-threaded environment.
Hibernate SessionFactory openStatelessSession
The openStatelessSession() method returns StatelessSession object. There is also another method that takes java.sql.Connection object as parammter to get a StatelessSession object from hibernate.
In Hibernate, StatelessSession does not implement first-level cache and it doesn’t support for any second-level cache. Because it’s stateless so it doesn’t support transactional write-behind, automatic dirty checking and do cascading operations.
Collections are ignored by a stateless session. Operations performed via a stateless session bypass Hibernate’s event model and interceptors. Stateless sessions are vulnerable to data aliasing effects, due to the lack of a first-level cache. Actually, Stateless session is more like a normal JDBC connection and doesn’t provide any benefits of hibernate framework.
However, for certain kinds of transactions, a stateless session may perform slightly faster than a stateful session. For example, we can use stateless session object in case of loading bulk data into database. This solution will avoid to holding huge data in first-level cache memory.
That’s it for the Hibernate SessionFactory Example tutorial.
Download complete source code, please click link below
HibernateSessionExample.zip (74 downloads)