Hibernate SessionFactory Example

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().

Hibernate SessionFactory Example


Other interesting posts you may like

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

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 (43 downloads)

Leave a Comment


Please share it if you found this useful
Hide Buttons