Interface SessionFactory
-
- All Superinterfaces:
AutoCloseable,Closeable,EntityManagerFactory,Referenceable,Serializable
- All Known Subinterfaces:
SessionFactoryImplementor
- All Known Implementing Classes:
SessionFactoryDelegatingImpl
public interface SessionFactory extends EntityManagerFactory, Referenceable, Serializable, Closeable
ASessionFactoryrepresents 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 singleSessionFactoryinstance, and must obtain a newSessioninstance from the factory each time it services a client request.Depending on how Hibernate is configured, the
SessionFactoryitself 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
SessionFactoryis 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 theSessionFactoryis created. Of course, anySessionFactoryis threadsafe.Every
SessionFactoryis a JPAEntityManagerFactory. Furthermore, when Hibernate is acting as the JPA persistence provider, the methodEntityManagerFactory.unwrap(Class)may be used to obtain the underlyingSessionFactory.- See Also:
Session,Configuration
-
-
Nested Class Summary
Nested Classes Modifier and Type Interface Description static classSessionFactory.TransactionManagementException
-
Method Summary
All Methods Instance Methods Abstract Methods Default Methods Deprecated Methods Modifier and Type Method Description voidclose()Destroy thisSessionFactoryand release all its resources, including caches and connection pools.booleancontainsFetchProfileDefinition(String name)Determine if this session factory contains a fetch profile definition registered under the given name.<T> List<EntityGraph<? super T>>findEntityGraphsByType(Class<T> entityClass)Return allEntityGraphs registered for the given entity type.default <R> RfromSession(Function<Session,R> action)Open a Session and perform an action using it.default <R> RfromTransaction(Function<Session,R> action)Open aSessionand perform an action using the session within the bounds of a transaction.CachegetCache()Obtain direct access to the underlying cache regions.ClassMetadatagetClassMetadata(Class entityClass)Deprecated.Use the descriptors fromEntityManagerFactory.getMetamodel()insteadClassMetadatagetClassMetadata(String entityName)Deprecated.Use the descriptors fromEntityManagerFactory.getMetamodel()insteadCollectionMetadatagetCollectionMetadata(String roleName)Deprecated.Use the descriptors fromEntityManagerFactory.getMetamodel()insteadSessiongetCurrentSession()Obtains the current session, an instance ofSessionimplicitly associated with some context.Set<String>getDefinedFilterNames()Obtain the set of names of alldefined filters.FilterDefinitiongetFilterDefinition(String filterName)Obtain the definition of a filter by name.SessionFactoryOptionsgetSessionFactoryOptions()Get the special options used to build the factory.StatisticsgetStatistics()Retrieve the statistics for this factory.default voidinSession(Consumer<Session> action)Open a Session and perform a action using itdefault voidinTransaction(Consumer<Session> action)Open aSessionand perform an action using the session within the bounds of a transaction.booleanisClosed()Is this factory already closed?SessionopenSession()Open aSession.StatelessSessionopenStatelessSession()Open a new stateless session.StatelessSessionopenStatelessSession(Connection connection)Open a new stateless session, utilizing the specified JDBCConnection.SessionBuilderwithOptions()Obtain a session builder for creating newSessions with certain customized options.StatelessSessionBuilderwithStatelessOptions()Obtain aStatelessSessionbuilder.-
Methods inherited from interface jakarta.persistence.EntityManagerFactory
addNamedEntityGraph, addNamedQuery, createEntityManager, createEntityManager, createEntityManager, createEntityManager, getCriteriaBuilder, getMetamodel, getPersistenceUnitUtil, getProperties, isOpen, unwrap
-
Methods inherited from interface javax.naming.Referenceable
getReference
-
-
-
-
Method Detail
-
getSessionFactoryOptions
SessionFactoryOptions getSessionFactoryOptions()
Get the special options used to build the factory.- Returns:
- The special options used to build the factory.
-
withOptions
SessionBuilder withOptions()
Obtain a session builder for creating newSessions with certain customized options.- Returns:
- The session builder
-
openSession
Session openSession() throws HibernateException
Open aSession. Any JDBCconnectionwill be obtained lazily from theConnectionProvideras needed to perform requested work.- Returns:
- The created session.
- Throws:
HibernateException- Indicates a problem opening the session; pretty rare here.
-
getCurrentSession
Session getCurrentSession() throws HibernateException
Obtains the current session, an instance ofSessionimplicitly associated with some context. For example, the session might be associated with the current thread, or with the current JTA transaction.The context used for scoping the current session (that is, the definition of what precisely "current" means here) is determined by an implementation of
CurrentSessionContext. An implementation may be selected using the configuration property "hibernate.current_session_context_class".If no
CurrentSessionContextis explicitly configured, but JTA is configured, thenJTASessionContextis used.- Returns:
- The current session.
- Throws:
HibernateException- Indicates an issue locating a suitable current session.
-
withStatelessOptions
StatelessSessionBuilder withStatelessOptions()
Obtain aStatelessSessionbuilder.- Returns:
- The stateless session builder
-
openStatelessSession
StatelessSession openStatelessSession()
Open a new stateless session.- Returns:
- The created stateless session.
-
openStatelessSession
StatelessSession openStatelessSession(Connection connection)
Open a new stateless session, utilizing the specified JDBCConnection.- Parameters:
connection- Connection provided by the application.- Returns:
- The created stateless session.
-
inSession
default void inSession(Consumer<Session> action)
Open a Session and perform a action using it
-
inTransaction
default void inTransaction(Consumer<Session> action)
Open aSessionand perform an action using the session within the bounds of a transaction.
-
fromSession
default <R> R fromSession(Function<Session,R> action)
Open a Session and perform an action using it.
-
fromTransaction
default <R> R fromTransaction(Function<Session,R> action)
Open aSessionand perform an action using the session within the bounds of a transaction.
-
getStatistics
Statistics getStatistics()
Retrieve the statistics for this factory.- Returns:
- The statistics.
-
close
void close() throws HibernateExceptionDestroy thisSessionFactoryand release all its resources, including caches and connection pools. 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.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Specified by:
closein interfaceEntityManagerFactory- Throws:
HibernateException- Indicates an issue closing the factory.
-
isClosed
boolean isClosed()
Is this factory already closed?- Returns:
- True if this factory is already closed; false otherwise.
-
getCache
Cache getCache()
Obtain direct access to the underlying cache regions.- Specified by:
getCachein interfaceEntityManagerFactory- Returns:
- The direct cache access API.
-
findEntityGraphsByType
<T> List<EntityGraph<? super T>> findEntityGraphsByType(Class<T> entityClass)
Return allEntityGraphs registered for the given entity type.
-
getDefinedFilterNames
Set<String> getDefinedFilterNames()
Obtain the set of names of alldefined filters.- Returns:
- The set of filter names.
-
getFilterDefinition
FilterDefinition getFilterDefinition(String filterName) throws HibernateException
Obtain the definition of a filter by name.- Parameters:
filterName- The name of the filter for which to obtain the definition.- Returns:
- The filter definition.
- Throws:
HibernateException- If no filter defined with the given name.
-
containsFetchProfileDefinition
boolean containsFetchProfileDefinition(String name)
Determine if this session factory contains a fetch profile definition registered under the given name.- Parameters:
name- The name to check- Returns:
- True if there is such a fetch profile; false otherwise.
-
getClassMetadata
@Deprecated ClassMetadata getClassMetadata(Class entityClass)
Deprecated.Use the descriptors fromEntityManagerFactory.getMetamodel()insteadRetrieve theClassMetadataassociated with the given entity class.- Parameters:
entityClass- The entity class- Returns:
- The metadata associated with the given entity; may be null if no such entity was mapped.
- Throws:
HibernateException- Generally null is returned instead of throwing.
-
getClassMetadata
@Deprecated ClassMetadata getClassMetadata(String entityName)
Deprecated.Use the descriptors fromEntityManagerFactory.getMetamodel()insteadRetrieve theClassMetadataassociated with the given entity class.- Parameters:
entityName- The entity class- Returns:
- The metadata associated with the given entity; may be null if no such entity was mapped.
- Throws:
HibernateException- Generally null is returned instead of throwing.- Since:
- 3.0
-
getCollectionMetadata
@Deprecated CollectionMetadata getCollectionMetadata(String roleName)
Deprecated.Use the descriptors fromEntityManagerFactory.getMetamodel()insteadGet theCollectionMetadataassociated with the named collection role.- Parameters:
roleName- The collection role (in form [owning-entity-name].[collection-property-name]).- Returns:
- The metadata associated with the given collection; may be null if no such collection was mapped.
- Throws:
HibernateException- Generally null is returned instead of throwing.
-
-