Flat Preloader Icon

Hibernate – Sessions


  • Before you can work with sessions in Hibernate, you need to create a SessionFactory instance. The SessionFactory is a heavyweight object that is typically created once during the application’s startup and shared across multiple parts of your application. It represents a configuration for the database connection and various settings needed for Hibernate.

Opening a Session:

  • To work with a database in Hibernate, you create and open a Session object. A session is a lightweight, short-lived object that is obtained from the SessionFactory. You typically create a new Session for each unit of work and close it when you are done.
					SessionFactory sessionFactory = 
// Initialize the SessionFactory
Session session = sessionFactory.openSession();
try {
    // Perform database operations using the session
} finally {
    // Always close the session to release resources


CRUD Operations:

  • With an open session, you can perform various CRUD (Create, Read, Update, Delete) operations on your domain objects. Hibernate will track changes made to objects within the session and automatically synchronize those changes with the database when necessary.
					// Example of saving an object
Transaction transaction = session.beginTransaction();
try {
} catch (Exception e) {


Transaction Management:

  • Sessions are typically used within transactions. You begin a transaction, perform database operations, and then either commit the transaction (if everything succeeds) or roll it back (if an error occurs). Hibernate can work with both programmatic and declarative transaction management.
					Transaction transaction = session.beginTransaction();
try {
    // Perform database operations
    // Commit the transaction if successful
} catch (Exception e) {
    // Roll back the transaction on error

  • transient − A new instance of a persistent class, which is not associated with a Session and has no representation in the database and no identifier value is considered transient by Hibernate.
  • persistent − You can make a transient instance persistent by associating it with a Session. A persistent instance has a representation in the database, an identifier value and is associated with a Session.
  • detached − Once we close the Hibernate Session, the persistent instance will become a detached instance.
  • You can check Hibernate documentation for a complete list of methods associated with Session and SessionFactory..
Sr.No. Session Methods Description
1. Transaction beginTransaction() Begin a unit of work and return the associated Transaction object.
2. void cancelQuery() Cancel the execution of the current query.
3. void clear() Completely clear the session.
4. Connection close() End the session by releasing the JDBC connection and cleaning up.
5. Criteria createCriteria(Class persistentClass) Create a new Criteria instance, for the given entity class, or a superclass of an entity class.
6. Criteria createCriteria(String entityName) Create a new Criteria instance, for the given entity name.
7. Serializable getIdentifier(Object object) Return the identifier value of the given entity as associated with this session.
8. Query createFilter(Object collection, String queryString) Create a new instance of Query for the given collection and filter string.
9. Query createQuery(String queryString) Create a new instance of Query for the given HQL query string.
10. SQLQuery createSQLQuery(String queryString) Create a new instance of SQLQuery for the given SQL query string.
11. void delete(Object object) Remove a persistent instance from the datastore.
12. void delete(String entityName, Object object) Remove a persistent instance from the datastore.
13. Session get(String entityName, Serializable id) Return the persistent instance of the given named entity with the given identifier, or null if there is no such persistent instance..
14. SessionFactory getSessionFactory() Get the session factory which created this session.
15. void refresh(Object object) Re-read the state of the given instance from the underlying database.