public final class

SessionFactoryImpl

extends Object
implements SessionFactory SessionFactoryImplementor
java.lang.Object
   ↳ org.hibernate.impl.SessionFactoryImpl

Class Overview

Concrete implementation of the SessionFactory interface. Has the following responsibilities

  • caches configuration settings (immutably)
  • caches "compiled" mappings ie. EntityPersisters and CollectionPersisters (immutable)
  • caches "compiled" queries (memory sensitive cache)
  • manages PreparedStatements
  • delegates JDBC Connection management to the ConnectionProvider
  • factory for instances of SessionImpl
This class must appear immutable to clients, even if it does all kinds of caching and pooling under the covers. It is crucial that the class is not only thread safe, but also highly concurrent. Synchronization must be used extremely sparingly.

Summary

Public Constructors
SessionFactoryImpl(Configuration cfg, Mapping mapping, Settings settings, EventListeners listeners, SessionFactoryObserver observer)
Public Methods
void close()
Closes the session factory, releasing all held resources.
boolean containsFetchProfileDefinition(String name)
Determine if this session factory contains a fetch profile definition registered under the given name.
void evict(Class persistentClass, Serializable id)
Evict an entry from the second-level cache.
void evict(Class persistentClass)
Evict all entries from the second-level cache.
void evictCollection(String roleName)
Evict all entries from the second-level cache.
void evictCollection(String roleName, Serializable id)
Evict an entry from the second-level cache.
void evictEntity(String entityName, Serializable id)
Evict an entry from the second-level cache.
void evictEntity(String entityName)
Evict all entries from the second-level cache.
void evictQueries()
Evict any query result sets cached in the default query cache region.
void evictQueries(String regionName)
Evict any query result sets cached in the named query cache region.
Map<StringClassMetadata> getAllClassMetadata()
Retrieve the ClassMetadata for all mapped entities.
Map getAllCollectionMetadata()
Get the CollectionMetadata for all mapped collections
Map getAllSecondLevelCacheRegions()
Get a map of all the second level cache regions currently maintained in this session factory.
BatcherFactory getBatcherFactory()
Cache getCache()
Obtain direct access to the underlying cache regions.
ClassMetadata getClassMetadata(Class persistentClass)
Retrieve the ClassMetadata associated with the given entity class.
ClassMetadata getClassMetadata(String entityName)
Retrieve the ClassMetadata associated with the given entity class.
CollectionMetadata getCollectionMetadata(String roleName)
Get the CollectionMetadata associated with the named collection role.
CollectionPersister getCollectionPersister(String role)
Get the persister object for a collection role.
Set<String> getCollectionRolesByEntityParticipant(String entityName)
Retrieves a set of all the collection roles in which the given entity is a participant, as either an index or an element.
ConnectionProvider getConnectionProvider()
Get the connection provider
Session getCurrentSession()
Obtains the current session.
Set getDefinedFilterNames()
Obtain a set of the names of all filters defined on this SessionFactory.
Dialect getDialect()
Get the SQL dialect.
EntityNotFoundDelegate getEntityNotFoundDelegate()
EntityPersister getEntityPersister(String entityName)
Get the persister for the named entity
EventListeners getEventListeners()
SessionFactoryObserver getFactoryObserver()
Return the SessionFactoryObserver attached to the SessionFactory if any
FetchProfile getFetchProfile(String name)
Retrieve fetch profile by name.
FilterDefinition getFilterDefinition(String filterName)
Obtain the definition of a filter by name.
IdentifierGenerator getIdentifierGenerator(String rootEntityName)
Get the identifier generator for the hierarchy
IdentifierGeneratorFactory getIdentifierGeneratorFactory()
Allow access to the id generator factory, though this is only needed/allowed from configuration.@return
String getIdentifierPropertyName(String className)
Type getIdentifierType(String className)
String[] getImplementors(String className)
Return the names of all persistent (mapped) classes that extend or implement the given class or interface, accounting for implicit/explicit polymorphism settings and excluding mapped subclasses/joined-subclasses of other classes in the result.
String getImportedClassName(String className)
Get a class name, using query language imports
Interceptor getInterceptor()
Get the factory scoped interceptor for this factory.
NamedQueryDefinition getNamedQuery(String queryName)
NamedSQLQueryDefinition getNamedSQLQuery(String queryName)
Properties getProperties()
Get a copy of the Properties used to configure this session factory.
QueryCache getQueryCache()
Get the default query cache
QueryCache getQueryCache(String regionName)
Get a particular named query cache, or the default cache
QueryPlanCache getQueryPlanCache()
Reference getReference()
Type getReferencedPropertyType(String className, String propertyName)
ResultSetMappingDefinition getResultSetMapping(String resultSetName)
String[] getReturnAliases(String queryString)
Get the return aliases of a query
Type[] getReturnTypes(String queryString)
Get the return types of a query
SQLExceptionConverter getSQLExceptionConverter()
Retrieves the SQLExceptionConverter in effect for this SessionFactory.
Region getSecondLevelCacheRegion(String regionName)
Get a named second-level cache region
Settings getSettings()
SQLFunctionRegistry getSqlFunctionRegistry()
Statistics getStatistics()
Retrieve the statistics fopr this factory.
StatisticsImplementor getStatisticsImplementor()
Statistics SPI
TransactionFactory getTransactionFactory()
TransactionManager getTransactionManager()
Get the JTA transaction manager
TypeHelper getTypeHelper()
Retrieve this factory's TypeHelper
TypeResolver getTypeResolver()
Retrieve the Type resolver associated with this factory.
UpdateTimestampsCache getUpdateTimestampsCache()
Get the cache of table update timestamps
boolean isClosed()
Is this factory already closed?
Iterator iterateEntityNameResolvers(EntityMode entityMode)
Session openSession(Connection connection, boolean flushBeforeCompletionEnabled, boolean autoCloseSessionEnabled, ConnectionReleaseMode connectionReleaseMode)
Open a session conforming to the given parameters.
Session openSession(Connection connection)
Open a Session, utilizing the specfied JDBC Connection.
Session openSession(Connection connection, Interceptor sessionLocalInterceptor)
Open a Session, utilizing the specfied JDBC Connection and specified Interceptor.
Session openSession()
Open a Session.
Session openSession(Interceptor sessionLocalInterceptor)
Open a Session, utilizing the specified Interceptor.
StatelessSession openStatelessSession(Connection connection)
Open a new stateless session, utilizing the specified JDBC Connection.
StatelessSession openStatelessSession()
Open a new stateless session.
Session openTemporarySession()
Get a nontransactional "current" session for Hibernate EntityManager
void registerEntityNameResolver(EntityNameResolver resolver, EntityMode entityMode)
[Expand]
Inherited Methods
From class java.lang.Object
From interface javax.naming.Referenceable
From interface org.hibernate.SessionFactory
From interface org.hibernate.engine.Mapping
From interface org.hibernate.engine.SessionFactoryImplementor

Public Constructors

public SessionFactoryImpl (Configuration cfg, Mapping mapping, Settings settings, EventListeners listeners, SessionFactoryObserver observer)

Public Methods

public void close ()

Closes the session factory, releasing all held resources.

  1. cleans up used cache regions and "stops" the cache provider.
  2. close the JDBC connection
  3. remove the JNDI binding
Note: Be aware that the sessionfactory instance still can be a "heavy" object memory wise after close() has been called. Thus it is important to not keep referencing the instance to let the garbage collector release the memory.

public 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.

public void evict (Class persistentClass, Serializable id)

Evict an entry from the second-level cache. This method occurs outside of any transaction; it performs an immediate "hard" remove, so does not respect any transaction isolation semantics of the usage strategy. Use with care.

Parameters
persistentClass The entity class for which to evict data.
id The entity id

public void evict (Class persistentClass)

Evict all entries from the second-level cache. This method occurs outside of any transaction; it performs an immediate "hard" remove, so does not respect any transaction isolation semantics of the usage strategy. Use with care.

Parameters
persistentClass The entity class for which to evict data.

public void evictCollection (String roleName)

Evict all entries from the second-level cache. This method occurs outside of any transaction; it performs an immediate "hard" remove, so does not respect any transaction isolation semantics of the usage strategy. Use with care.

Parameters
roleName The name of the collection role whose regions should be evicted

public void evictCollection (String roleName, Serializable id)

Evict an entry from the second-level cache. This method occurs outside of any transaction; it performs an immediate "hard" remove, so does not respect any transaction isolation semantics of the usage strategy. Use with care.

Parameters
roleName The name of the collection role
id The id of the collection owner

public void evictEntity (String entityName, Serializable id)

Evict an entry from the second-level cache. This method occurs outside of any transaction; it performs an immediate "hard" remove, so does not respect any transaction isolation semantics of the usage strategy. Use with care.

Parameters
entityName The entity name for which to evict data.
id The entity id

public void evictEntity (String entityName)

Evict all entries from the second-level cache. This method occurs outside of any transaction; it performs an immediate "hard" remove, so does not respect any transaction isolation semantics of the usage strategy. Use with care.

Parameters
entityName The entity name for which to evict data.

public void evictQueries ()

Evict any query result sets cached in the default query cache region.

public void evictQueries (String regionName)

Evict any query result sets cached in the named query cache region.

Parameters
regionName The named query cache region from which to evict.

public Map<StringClassMetadata> getAllClassMetadata ()

Retrieve the ClassMetadata for all mapped entities.

Returns

public Map getAllCollectionMetadata ()

Get the CollectionMetadata for all mapped collections

Returns
  • a map from String to CollectionMetadata

public Map getAllSecondLevelCacheRegions ()

Get a map of all the second level cache regions currently maintained in this session factory. The map is structured with the region name as the key and the Region instances as the values.

Returns
  • The map of regions

public BatcherFactory getBatcherFactory ()

public Cache getCache ()

Obtain direct access to the underlying cache regions.

Returns
  • The direct cache access API.

public ClassMetadata getClassMetadata (Class persistentClass)

Retrieve the ClassMetadata associated with the given entity class.

Parameters
persistentClass The entity class
Returns
  • The metadata associated with the given entity; may be null if no such entity was mapped.

public ClassMetadata getClassMetadata (String entityName)

Retrieve the ClassMetadata associated 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.

public CollectionMetadata getCollectionMetadata (String roleName)

Get the CollectionMetadata associated 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.

public CollectionPersister getCollectionPersister (String role)

Get the persister object for a collection role.

Parameters
role The role (name) of the collection for which to retrieve the persister.
Returns
  • The persister

public Set<String> getCollectionRolesByEntityParticipant (String entityName)

Retrieves a set of all the collection roles in which the given entity is a participant, as either an index or an element.

Parameters
entityName The entity name for which to get the collection roles.
Returns
  • set of all the collection roles in which the given entityName participates.

public ConnectionProvider getConnectionProvider ()

Get the connection provider

public Session getCurrentSession ()

Obtains the current session. The definition of what exactly "current" means controlled by the CurrentSessionContext impl configured for use.

Note that for backwards compatibility, if a CurrentSessionContext is not configured but a JTA TransactionManagerLookup is configured this will default to the JTASessionContext impl.

Returns
  • The current session.

public Set getDefinedFilterNames ()

Obtain a set of the names of all filters defined on this SessionFactory.

Returns
  • The set of filter names.

public Dialect getDialect ()

Get the SQL dialect.

Shorthand for getSettings().getDialect()

Returns
  • The dialect

public EntityNotFoundDelegate getEntityNotFoundDelegate ()

public EntityPersister getEntityPersister (String entityName)

Get the persister for the named entity

Parameters
entityName The name of the entity for which to retrieve the persister.
Returns
  • The persister

public EventListeners getEventListeners ()

public SessionFactoryObserver getFactoryObserver ()

Return the SessionFactoryObserver attached to the SessionFactory if any

public FetchProfile getFetchProfile (String name)

Retrieve fetch profile by name.

Parameters
name The name of the profile to retrieve.
Returns
  • The profile definition

public FilterDefinition getFilterDefinition (String filterName)

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.

public IdentifierGenerator getIdentifierGenerator (String rootEntityName)

Get the identifier generator for the hierarchy

public IdentifierGeneratorFactory getIdentifierGeneratorFactory ()

Allow access to the id generator factory, though this is only needed/allowed from configuration.@return

public String getIdentifierPropertyName (String className)

public Type getIdentifierType (String className)

public String[] getImplementors (String className)

Return the names of all persistent (mapped) classes that extend or implement the given class or interface, accounting for implicit/explicit polymorphism settings and excluding mapped subclasses/joined-subclasses of other classes in the result.

public String getImportedClassName (String className)

Get a class name, using query language imports

public Interceptor getInterceptor ()

Get the factory scoped interceptor for this factory.

Returns
  • The factory scope interceptor, or null if none.

public NamedQueryDefinition getNamedQuery (String queryName)

public NamedSQLQueryDefinition getNamedSQLQuery (String queryName)

public Properties getProperties ()

Get a copy of the Properties used to configure this session factory.

Returns
  • The properties.

public QueryCache getQueryCache ()

Get the default query cache

public QueryCache getQueryCache (String regionName)

Get a particular named query cache, or the default cache

Parameters
regionName the name of the cache region, or null for the default query cache
Returns
  • the existing cache, or a newly created cache if none by that region name

public QueryPlanCache getQueryPlanCache ()

public Reference getReference ()

public Type getReferencedPropertyType (String className, String propertyName)

public ResultSetMappingDefinition getResultSetMapping (String resultSetName)

public String[] getReturnAliases (String queryString)

Get the return aliases of a query

public Type[] getReturnTypes (String queryString)

Get the return types of a query

public SQLExceptionConverter getSQLExceptionConverter ()

Retrieves the SQLExceptionConverter in effect for this SessionFactory.

Returns
  • The SQLExceptionConverter for this SessionFactory.

public Region getSecondLevelCacheRegion (String regionName)

Get a named second-level cache region

Parameters
regionName The name of the region to retrieve.
Returns
  • The region

public Settings getSettings ()

public SQLFunctionRegistry getSqlFunctionRegistry ()

public Statistics getStatistics ()

Retrieve the statistics fopr this factory.

Returns
  • The statistics.

public StatisticsImplementor getStatisticsImplementor ()

Statistics SPI

public TransactionFactory getTransactionFactory ()

public TransactionManager getTransactionManager ()

Get the JTA transaction manager

public TypeHelper getTypeHelper ()

Retrieve this factory's TypeHelper

Returns

public TypeResolver getTypeResolver ()

Retrieve the Type resolver associated with this factory.

Returns
  • The type resolver

public UpdateTimestampsCache getUpdateTimestampsCache ()

Get the cache of table update timestamps

public boolean isClosed ()

Is this factory already closed?

Returns
  • True if this factory is already closed; false otherwise.

public Iterator iterateEntityNameResolvers (EntityMode entityMode)

public Session openSession (Connection connection, boolean flushBeforeCompletionEnabled, boolean autoCloseSessionEnabled, ConnectionReleaseMode connectionReleaseMode)

Open a session conforming to the given parameters. Used mainly by JTASessionContext for current session processing.

Parameters
connection The external jdbc connection to use, if one (i.e., optional).
flushBeforeCompletionEnabled Should the session be auto-flushed prior to transaction completion?
autoCloseSessionEnabled Should the session be auto-closed after transaction completion?
connectionReleaseMode The release mode for managed jdbc connections.
Returns
  • An appropriate session.

public Session openSession (Connection connection)

Open a Session, utilizing the specfied JDBC Connection.

Note that the second-level cache will be disabled if you supply a JDBC connection. Hibernate will not be able to track any statements you might have executed in the same transaction. Consider implementing your own ConnectionProvider instead as a highly recommended alternative.

Parameters
connection a connection provided by the application.
Returns
  • The created session.

public Session openSession (Connection connection, Interceptor sessionLocalInterceptor)

Open a Session, utilizing the specfied JDBC Connection and specified Interceptor.

Note that the second-level cache will be disabled if you supply a JDBC connection. Hibernate will not be able to track any statements you might have executed in the same transaction. Consider implementing your own ConnectionProvider instead as a highly recommended alternative.

Parameters
connection a connection provided by the application.
sessionLocalInterceptor a session-scoped interceptor
Returns
  • The created session.

public Session openSession ()

Open a Session.

JDBC connection(s will be obtained from the configured ConnectionProvider as needed to perform requested work.

Returns
  • The created session.

public Session openSession (Interceptor sessionLocalInterceptor)

Open a Session, utilizing the specified Interceptor.

JDBC connection(s will be obtained from the configured ConnectionProvider as needed to perform requested work.

Parameters
sessionLocalInterceptor a session-scoped interceptor
Returns
  • The created session.

public StatelessSession openStatelessSession (Connection connection)

Open a new stateless session, utilizing the specified JDBC Connection.

Parameters
connection Connection provided by the application.
Returns
  • The created stateless session.

public StatelessSession openStatelessSession ()

Open a new stateless session.

Returns
  • The created stateless session.

public Session openTemporarySession ()

Get a nontransactional "current" session for Hibernate EntityManager

public void registerEntityNameResolver (EntityNameResolver resolver, EntityMode entityMode)