SessionFactory is Hibernate’s native equivalent of JPA’s EntityManagerFactory. Applications usually create one from database connection settings and mapped classes, then use it to obtain Session instances. section 2.4
A native Hibernate setup typically involves:
- Configure database and session properties in hibernate.cfg.xml or hibernate.properties. section 2.4
- Open an explicit Session or obtain the current transaction-bound Session for database work. section 14.4
- Close explicitly opened sessions; transaction-managed current sessions are flushed and closed when the transaction ends. section 14.4
Building and using a factory
A typical bootstrap creates a Configuration, loads hibernate.cfg.xml, adds Message.class, builds a service registry, and returns configuration.buildSessionFactory(serviceRegistry). Application code can then use sessionFactory.getCurrentSession().get(Item.class, id) inside a transaction.
