SessionFactory represents an "instance" of Hibernate: it maintains
the runtime metamodel representing persistent entities, their attributes,
their associations, and their mappings to relational database tables, along
with configuration that
affects the runtime behavior of Hibernate, and instances of services that
Hibernate needs to perform its duties.
Crucially, this is where a program comes to obtain sessions.
Typically, a program has a single SessionFactory instance, and must
obtain a new Session instance from the factory each time it services
a client request. It is then also responsible for destroying the session at the end of the client request.
The inSession and inTransaction methods provide a convenient
way to obtain a session, with or without starting a transaction, and have it
cleaned up automatically, relieving the program of the need to explicitly
call Session#close() and Transaction#commit().
Alternatively, getCurrentSession() provides support for the notion
of contextually-scoped sessions, where an implementation of the SPI interface
org. is responsible for
creating, scoping, and destroying sessions.
Depending on how Hibernate is configured, the SessionFactory itself
might be responsible for the lifecycle of pooled JDBC connections and
transactions, or it may simply act as a client for a connection pool or
transaction manager provided by a container environment.
The internal state of a SessionFactory is considered in some sense
"immutable". While it interacts with stateful services like JDBC connection
pools, such state changes are never visible to its clients. In particular,
the runtime metamodel representing the entities and their O/R mappings is
fixed as soon as the SessionFactory is created. Of course, any
SessionFactory is threadsafe.
There are some interesting exceptions to this principle:
SessionFactory has its own isolated second-level cache,
shared between the sessions it creates, and it exposes the cache to clients as a stateful object with entries that
may be queried and managed directly.
Statistics object which accumulates information summarizing
the activity of sessions created by the factory. It provides statistics
about interactions with JDBC and with the second-level cache.
addNamedQuery(String, jakarta.persistence.Query)
and addNamedEntityGraph(String, EntityGraph) on the interface
EntityManagerFactory which SessionFactory inherits.
Of course, these methods are usually called at the time the
EntityManagerFactory is created. It's difficult to imagine a
motivation to call either method later, when the factory already has
active sessions.
The SessionFactory exposes part of the information in the runtime
metamodel via an instance of the JPA-defined
jakarta.. This object is sometimes
used in a sophisticated way by libraries or frameworks to implement generic
concerns involving entity classes.
When the Metamodel Generator is used, elements of this metamodel may also
be obtained in a typesafe way, via the generated metamodel classes. For
an entity class Book, the generated Book_ class has:
class_ of type
EntityType<Book>,
and
Book, for example,
title of type SingularAttribute<Book,String>.
Use of these statically-typed metamodel references is the preferred way of working with the criteria query API, and with EntityGraphs.
The factory also provides a
SchemaManager which allows, as a convenience for writing tests:
Finally, the factory provides a
HibernateCriteriaBuilder, an extension to the JPA-defined interface
jakarta., which may be used to
construct criteria
queries.
Every SessionFactory is a JPA EntityManagerFactory.
Furthermore, when Hibernate is acting as the JPA persistence provider, the
method EntityManagerFactory#unwrap(Class) may be used to obtain the
underlying SessionFactory.
The very simplest way to obtain a new SessionFactory is using a
org..
Session, org.hibernate.cfg.Configuration
| Modifier and Type | Method and Description |
|---|---|
| public void | close()
Redeclares jakarta. SessionFactory and release all its resources,
including caches and connection pools.
|
| public default boolean | Returns: True if there is such a fetch profile; false otherwise.The name to check name)Determine if there is a fetch profile definition registered under the given name. |
| public RootGraph | Returns: an instance ofRootGraphthe name given to some name)jakarta.Return the root |
| public <T> List | findEntityGraphsByType(Class<T> entityClass)
Return all |
| public default <R> R | |
| public default <R> R | fromStatelessSession(Function<StatelessSession, R> action)
Open a |
| public default <R> R | fromStatelessTransaction(Function<StatelessSession, R> action)
Open a |
| public default <R> R | fromTransaction(Function<Session, R> action)
Open a |
| public Cache | Returns: The direct cache access API.Redeclares jakarta. |
| public HibernateCriteriaBuilder | getCriteriaBuilder()
Redeclares jakarta. HibernateCriteriaBuilder which may be used to
construct
criteria
queries.
|
| public Session | Returns: The current session.Obtains the current session, an instance of |
| public Set | Returns: The set of fetch profile names given byorg. annotations.Obtain the set of names of all |
| public Set | Returns: The set of filter names given byorg. annotationsObtain the set of names of all |
| public FilterDefinition | Returns: The filter definition.The name of the filter for which to obtain the definition. filterName)
Deprecated
since 6.2. There is no plan to remove this operation, but its use should be
avoided since
Obtain the definition of a filter by name.
FilterDefinition is an SPI type, and so this
operation is a layer-breaker.
|
| public SchemaManager | getSchemaManager()
A |
| public SessionFactoryOptions | Returns: The special options used to build the factory.
Deprecated
since 6.2. There is no plan to remove this operation, but its use should be
avoided since
Get the options used to build this factory.
SessionFactoryOptions is an SPI type, and so
this operation is a layer-breaker.
|
| public Statistics | |
| public default void | |
| public default void | inStatelessSession(Consumer<StatelessSession> action)
Open a |
| public default void | inStatelessTransaction(Consumer<StatelessSession> action)
Open a |
| public default void | inTransaction(Consumer<Session> action)
Open a |
| public boolean | Returns: True if this factory is already closed; false otherwise.Is this factory already closed? |
| public Session | |
| public StatelessSession | |
| public StatelessSession | Returns: The created stateless session.Connection provided by the application. connection)Open a new stateless session, utilizing the specified JDBC
|
| public SessionBuilder | Returns: The session builderObtain a session builder for creating
new |
| public StatelessSessionBuilder |
| close | back to summary |
|---|---|
| public void close() throws HibernateException Redeclares jakarta. Destroy this It is the responsibility of the application to ensure that there are no open sessions before calling this method as the impact on those sessions is indeterminate. No-ops if already closed.
| |
| containsFetchProfileDefinition | back to summary |
|---|---|
| public default boolean containsFetchProfileDefinition(String name) Determine if there is a fetch profile definition registered under the given name.
| |
| findEntityGraphByName | back to summary |
|---|---|
| public RootGraph Return the root
| |
| findEntityGraphsByType | back to summary |
|---|---|
| public <T> List Return all
| |
| fromSession | back to summary |
|---|---|
| public default <R> R fromSession(Function<Session, R> action) Open a | |
| fromStatelessSession | back to summary |
|---|---|
| public default <R> R fromStatelessSession(Function<StatelessSession, R> action) Open a
| |
| fromStatelessTransaction | back to summary |
|---|---|
| public default <R> R fromStatelessTransaction(Function<StatelessSession, R> action) Open a
| |
| fromTransaction | back to summary |
|---|---|
| public default <R> R fromTransaction(Function<Session, R> action) Open a | |
| getCache | back to summary |
|---|---|
| public Cache getCache() Redeclares jakarta. Obtain direct access to the underlying cache regions. | |
| getCriteriaBuilder | back to summary |
|---|---|
| public HibernateCriteriaBuilder getCriteriaBuilder() Redeclares jakarta. Obtain a
| |
| getCurrentSession | back to summary |
|---|---|
| public Session getCurrentSession() throws HibernateException Obtains the current session, an instance of
The context used for scoping the current session (that is, the
definition of what precisely "current" means here) is determined
by an implementation of
If no
| |
| getDefinedFetchProfileNames | back to summary |
|---|---|
| public Set Obtain the set of names of all
| |
| getDefinedFilterNames | back to summary |
|---|---|
| public Set Obtain the set of names of all
| |
| getFilterDefinition | back to summary |
|---|---|
| public FilterDefinition getFilterDefinition(String filterName) throws HibernateException
Deprecated since 6.2. There is no plan to remove this operation, but its use should be
avoided since Obtain the definition of a filter by name.
| |
| getSchemaManager | back to summary |
|---|---|
| public SchemaManager getSchemaManager() A
| |
| getSessionFactoryOptions | back to summary |
|---|---|
| public SessionFactoryOptions getSessionFactoryOptions()
Deprecated since 6.2. There is no plan to remove this operation, but its use should be
avoided since Get the options used to build this factory.
| |
| getStatistics | back to summary |
|---|---|
| public Statistics getStatistics() Retrieve the statistics for this factory.
| |
| inSession | back to summary |
|---|---|
| public default void inSession(Consumer<Session> action) Open a | |
| inStatelessSession | back to summary |
|---|---|
| public default void inStatelessSession(Consumer<StatelessSession> action) Open a
| |
| inStatelessTransaction | back to summary |
|---|---|
| public default void inStatelessTransaction(Consumer<StatelessSession> action) Open a
| |
| inTransaction | back to summary |
|---|---|
| public default void inTransaction(Consumer<Session> action) Open a | |
| isClosed | back to summary |
|---|---|
| public boolean isClosed() Is this factory already closed?
| |
| openSession | back to summary |
|---|---|
| public Session openSession() throws HibernateException Open a
Any JDBC
| |
| openStatelessSession | back to summary |
|---|---|
| public StatelessSession openStatelessSession() Open a new stateless session.
| |
| openStatelessSession | back to summary |
|---|---|
| public StatelessSession openStatelessSession(Connection connection) Open a new stateless session, utilizing the specified JDBC
| |
| withOptions | back to summary |
|---|---|
| public SessionBuilder withOptions() Obtain a session builder for creating
new
| |
| withStatelessOptions | back to summary |
|---|---|
| public StatelessSessionBuilder withStatelessOptions() Obtain a
| |