What is the role of Session object in Hibernate?
Answered
What is the role of Session object in Hibernate?Is it thread-safe?
Best answer
Session object represents a unit of work in Hibernate that represents conversation between application and database. It is used to maintain connection with database. Session object is referred to as persistence manager since it is responsible for storing and retrieving object to and from database.
Session object is single threaded and hence it is not thread-safe. It can’t be shared between multiple threads.
Session object is created with the SessionFactory object:
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();