How to handle session in Hibernate

In the previous article, we have seen What is Configuration in Hibernate you can check it here How to configure hibernate in Java application. In this article, we will cover How to handle session in Hibernate?

As we know, in order to have a physical connection with the database in an application we need session objects. In this article, we will learn about the session object in detail.

What is Hibernate Session?

In Hibernate, Session is created to connect an application with the database physically. Whenever there is a need of connecting or retrieving any data from the database we need a session object to be created.

  • The session is an interface whose Object is provided with SessionFactory.
  • Objects that are saved in the database are retrieved using session objects.
  • the session object is instantiated when SessionFactory is created by the configuration object.

An instance of a session exists in the following state

  • transient: A transient state means the POJO class will not be associated with the session, with no schema in the database and with no identifier. 
  • persistent: A persistent state means the POJO class having a session, with schema in the database and with an identifier value.
  • detached: This state comes after a persistent state and has no session associate with it.

What are the Methods of Session Interface?

Let’s see some methods of Session Interface in Hibernate.

  • save(Object obj): This method generates the primary key and inserts the record in the database.
  • update(Object obj): This method updates the record in the database.
  • saveorUpdate(Object obj): This method saves or updates based on the object passed.
  • isOpen(): This method returns a boolean value and checks whether the session is open or not.
  • isDirty(): This method returns a boolean value and checks whether there are any changes in the session.
  • isConnected(): This method returns boolean values and checks whether the session is connected or not.
  • merge(): This method changes the state from detached to persistent.
  • delete(String entity, Object obj): This method deletes the persistent instance.
  • clear(): This method clears the session.
  • beginTransaction(): This method starts the transaction and returns the object of the Transaction.
  • cancelQuery(): This method cancels the current query execution.
  • createQuery(String sql): This method creates a new instance of Query.
  • createFilter(Object Collection, String sql): This method creates a new instance of Query for Filter String.
  • get(String entity, Serializable id): This method returns the persistence state of the given Entity.
  • refresh(Object obj): This method re-populates the entity with the latest data.
  • getSessionFactory(): This method returns the session Factory who created the session.
  • getIdentifier(Object obj): This method returns the identifier value associated with the session.

Example of creating Hibernate Session

Just create a simple project and add the hibernate jar files and Mysql jar file in the lib folder as we have done earlier you can check it here Environment Set up for Hibernate and then we will just create a session.

public class SessioninHibernate {

  public static void main(String[] args) {
        Student student=new Student();
        Configuration configuration = new Configuration();
        //create session Factory using configuration
    sessionFactory sf = configuration.buildSessionFactory();
       //open session
    Session session=sf.OpenSession();
       // Open transaction
   Transaction t=session.beginTransaction();


Advantages of Hibernate Session

  • It improves the efficiency of coding.
  • Prior knowledge of SQL in detail is not necessary only basic will help.
  • Minimal coding because everything is handle by classes and objects.
  • It is database independent.
  • More importantly, it follows the basic ACID property of the database.

Thus we have seen How to create a session in Hibernate.

In the next article of this tutorial, we will cover the POJO class in Hibernate with a simple example.