liveBook · Manning

SessionFactory

As explained in Java Persistence with Spring Data and Hibernate epub

SessionFactory is Hibernate's thread-safe factory for creating Session instances and managing Hibernate persistence resources.

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.

How Java Persistence with Spring Data and Hibernate epub covers it

How deep it goes
Explains configuration, construction, API switching, and session lifecycle, but defers most routine examples to JPA or Spring Data JPA.
Examples
Uses hibernate.cfg.xml, annotated Message and Item classes, Java bootstrap code, DAO access, and Spring’s LocalSessionFactoryBean.
What it assumes
Assumes core Java, persistent classes, database configuration, and basic JPA concepts while demonstrating native Hibernate bootstrapping.
What you can do after
Build it from scratch using native Hibernate configuration, or switch from JPA through EntityManagerFactory.unwrap(SessionFactory.class).
How it connects
Connects SessionFactory with Hibernate configuration, Session, EntityManagerFactory, JPA unwrapping, transactions, and Spring dependency injection.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Java Persistence with Spring Data and Hibernate epub. The section links point to the parts of the book it draws on.