A SessionFactory is the heavyweight, thread-safe factory that an application builds once at startup. A Session is the lightweight object it creates for a single unit of work, opened and closed around each transaction.
The SessionFactory reads the configuration once and is immutable after that. It is expensive to create, so an application typically keeps one. It also holds the second-level cache, which every session it creates shares.
The Session is the interface between your Java code and Hibernate. It is inexpensive to create and is not thread-safe, so each request or thread gets its own and closes it when the work is done.
// SessionFactory - created once
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
// Session - created per transaction
Session session = sessionFactory.openSession();
// ... perform operations
session.close();
This answer doesn't lend itself to a diagram - it reads best . No credits were charged.
Why there's no diagram: “”
The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓
The diagram below the answer is the concept . Jump to it ↓