Session Factory Object


SessionFactory:

In the package org. Hibernate, SessionFactory is used to build Session Object. I.e., factory, Session object is constructed based on SessionFactory thing.

It is thread-safe and immutable by nature.

Session:

Hibernate Framework is used to establish a connection/session with database software. It is used to conduct CRUD operations on the database.

a session is created by calling factory.buildSession()

Session Factory Object, Lifecycle of Session

To establish a physical database connection, you must first establish a session. The Session object is designed to be lightweight and instantiated when a database interaction is required. Using a Session object, persistent objects are saved and retrieved.

Because session objects are not thread-safe, they should be generated and discarded as needed. Session instances can be created, read, and deleted.

At any given moment, instances may be in one of the following three states:

  • transient − It is deemed temporary by Hibernate if it is not associated with a Session, does not have a corresponding entry in the database, and does not have an identifier assigned to it.
  • persistent −By linking a transitory instance with a Session, you can make it continuous. Consistent instances have an associated Session and are stored in the database with an identifying value.
  • detached − persistent object becomes disconnected when we close the Hibernate Session.