Site icon NgDeveloper

Difference between Session and Sessionfactory Hibernate?

Difference between Session and sessionfactory in Hibernate?

Basically session will be created from the sessionfactory instances. I have listed down the few differences between the session and sessionfactory in hibernate.

Sessionfactory:

What is session factory & How to create a sessionfactory ?

Session:

How Session will be created from session factory [sample snippet]?

// creating the session from the sessionfactory
Session session = HibernateConfig.getSessionFactory().openSession();
// fetching
session.beginTransaction();
Author authorFetch = (Author)session.get(Author.class, 2);
System.out.println(authorFetch.getAuthorName());
System.out.println(authorFetch.getSiteName());
session.getTransaction().commit();
// closing the session factory session
session.close();
HibernateConfig.getSessionFactory().close();

Full Executable program can be found here

Exit mobile version