Top Description Methods
org.hibernate

public Interface SessionFactory

extends Referenceable, Serializable
Known Direct Subinterfaces
org.hibernate.engine.SessionFactoryImplementor
Known Direct Implementers
org.hibernate.jmx.SessionFactoryStub, org.hibernate.impl.SessionFactoryImpl
Imports
java.io.Serializable, java.sql.Connection, java.util.Map, .Set, javax.naming.Referenceable, org.hibernate.metadata.ClassMetadata, .CollectionMetadata, org.hibernate.stat.Statistics, org.hibernate.engine.FilterDefinition

The main contract here is the creation of Session instances. Usually an application has a single SessionFactory instance and threads servicing client requests obtain Session instances from this factory.

The internal state of a SessionFactory is immutable. Once it is created this internal state is set. This internal state includes all of the metadata about Object/Relational Mapping.

Implementors must be threadsafe.

Authors
Gavin King, Steve Ebersole
See Also
org.hibernate.cfg.Configuration

Method Summary

Modifier and TypeMethod and Description
public void
close()

Destroy this SessionFactory and release all resources (caches, connection pools, etc).

public boolean

Returns:

True if there is such a fetch profile; false otherwise.
containsFetchProfileDefinition
(String
The name to check
name
)

Determine if this session factory contains a fetch profile definition registered under the given name.

public void
evict(Class<T>
The entity class for which to evict data.
persistentClass
)

Deprecated Use Cache#evictEntityRegion(Class) accessed through getCache() instead.
Evict all entries from the second-level cache.
public void
evict(Class<T>
The entity class for which to evict data.
persistentClass
,
Serializable
The entity id
id
)

Deprecated Use Cache#containsEntity(Class, Serializable) accessed through getCache() instead.
Evict an entry from the second-level cache.
public void
evictCollection(String
The name of the collection role whose regions should be evicted
roleName
)

Deprecated Use Cache#evictCollectionRegion(String) accessed through getCache() instead.
Evict all entries from the second-level cache.
public void
evictCollection(String
The name of the collection role
roleName
,
Serializable
The id of the collection owner
id
)

Deprecated Use Cache#evictCollection(String, Serializable) accessed through getCache() instead.
Evict an entry from the second-level cache.
public void
evictEntity(String
The entity name for which to evict data.
entityName
)

Deprecated Use Cache#evictEntityRegion(String) accessed through getCache() instead.
Evict all entries from the second-level cache.
public void
evictEntity(String
The entity name for which to evict data.
entityName
,
Serializable
The entity id
id
)

Deprecated Use Cache#evictEntity(String, Serializable) accessed through getCache() instead.
Evict an entry from the second-level cache.
public void
evictQueries(String
The named query cache region from which to evict.
cacheRegion
)

Deprecated Use Cache#evictQueryRegion(String) accessed through getCache() instead.
Evict any query result sets cached in the named query cache region.
public void
evictQueries()

Deprecated Use Cache#evictQueryRegions accessed through getCache() instead.
Evict any query result sets cached in the default query cache region.
public Map<String, ClassMetadata>

Returns:

A map containing all ClassMetadata keyed by the corresponding String entity-name.
getAllClassMetadata
()

Retrieve the ClassMetadata for all mapped entities.

public Map<K, V>

Returns:

a map from String to CollectionMetadata
getAllCollectionMetadata
()

Get the CollectionMetadata for all mapped collections

public Cache

Returns:

The direct cache access API.
getCache
()

Obtain direct access to the underlying cache regions.

public ClassMetadata

Returns:

The metadata associated with the given entity; may be null if no such entity was mapped.
getClassMetadata
(Class<T>
The entity class
entityClass
)

Retrieve the ClassMetadata associated with the given entity class.

public ClassMetadata

Returns:

The metadata associated with the given entity; may be null if no such entity was mapped.
getClassMetadata
(String
The entity class
entityName
)

Retrieve the ClassMetadata associated with the given entity class.

public CollectionMetadata

Returns:

The metadata associated with the given collection; may be null if no such collection was mapped.
getCollectionMetadata
(String
The collection role (in form [owning-entity-name].[collection-property-name]).
roleName
)

Get the CollectionMetadata associated with the named collection role.

public Session

Returns:

The current session.
getCurrentSession
()

Obtains the current session.

public Set<E>

Returns:

The set of filter names.
getDefinedFilterNames
()

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

public FilterDefinition

Returns:

The filter definition.
getFilterDefinition
(String
The name of the filter for which to obtain the definition.
filterName
)

Obtain the definition of a filter by name.

public Statistics

Returns:

The statistics.
getStatistics
()

Retrieve the statistics fopr this factory.

public TypeHelper

Returns:

The factory's TypeHelper
getTypeHelper
()

Retrieve this factory's TypeHelper

public boolean

Returns:

True if this factory is already closed; false otherwise.
isClosed
()

Is this factory already closed?

public Session

Returns:

The created session.
openSession
()

Open a Session.

public Session

Returns:

The created session.
openSession
(Interceptor
a session-scoped interceptor
interceptor
)

Open a Session, utilizing the specified Interceptor.

public Session

Returns:

The created session.
openSession
(Connection
a connection provided by the application.
connection
)

Open a Session, utilizing the specfied JDBC Connection.

public Session

Returns:

The created session.
openSession
(Connection
a connection provided by the application.
connection
,
Interceptor
a session-scoped interceptor
interceptor
)

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

public StatelessSession

Returns:

The created stateless session.
openStatelessSession
()

Open a new stateless session.

public StatelessSession

Returns:

The created stateless session.
openStatelessSession
(Connection
Connection provided by the application.
connection
)

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

Inherited from javax.naming.Referenceable:
getReference

Method Detail

closeback to summary
public void close() throws HibernateException

Destroy this SessionFactory and release all resources (caches, connection pools, etc).

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.

Exceptions
HibernateException:
Indicates an issue closing the factory.
containsFetchProfileDefinitionback to summary
public boolean containsFetchProfileDefinition(String name)

Determine if this session factory contains a fetch profile definition registered under the given name.

Parameters
name:String

The name to check

Returns:boolean

True if there is such a fetch profile; false otherwise.

evictback to summary
public void evict(Class<T> persistentClass) throws HibernateException

Deprecated

Use Cache#evictEntityRegion(Class) accessed through getCache() instead.

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:Class<T>

The entity class for which to evict data.

Exceptions
HibernateException:
Generally will mean that either that 'persisttentClass' did not name a mapped entity or a problem communicating with underlying cache impl.
evictback to summary
public void evict(Class<T> persistentClass, Serializable id) throws HibernateException

Deprecated

Use Cache#containsEntity(Class, Serializable) accessed through getCache() instead.

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:Class<T>

The entity class for which to evict data.

id:Serializable

The entity id

Exceptions
HibernateException:
Generally will mean that either that 'persisttentClass' did not name a mapped entity or a problem communicating with underlying cache impl.
evictCollectionback to summary
public void evictCollection(String roleName) throws HibernateException

Deprecated

Use Cache#evictCollectionRegion(String) accessed through getCache() instead.

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:String

The name of the collection role whose regions should be evicted

Exceptions
HibernateException:
Generally will mean that either that 'roleName' did not name a mapped collection or a problem communicating with underlying cache impl.
evictCollectionback to summary
public void evictCollection(String roleName, Serializable id) throws HibernateException

Deprecated

Use Cache#evictCollection(String, Serializable) accessed through getCache() instead.

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:String

The name of the collection role

id:Serializable

The id of the collection owner

Exceptions
HibernateException:
Generally will mean that either that 'roleName' did not name a mapped collection or a problem communicating with underlying cache impl.
evictEntityback to summary
public void evictEntity(String entityName) throws HibernateException

Deprecated

Use Cache#evictEntityRegion(String) accessed through getCache() instead.

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:String

The entity name for which to evict data.

Exceptions
HibernateException:
Generally will mean that either that 'persisttentClass' did not name a mapped entity or a problem communicating with underlying cache impl.
evictEntityback to summary
public void evictEntity(String entityName, Serializable id) throws HibernateException

Deprecated

Use Cache#evictEntity(String, Serializable) accessed through getCache() instead.

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:String

The entity name for which to evict data.

id:Serializable

The entity id

Exceptions
HibernateException:
Generally will mean that either that 'persisttentClass' did not name a mapped entity or a problem communicating with underlying cache impl.
evictQueriesback to summary
public void evictQueries(String cacheRegion) throws HibernateException

Deprecated

Use Cache#evictQueryRegion(String) accessed through getCache() instead.

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

Parameters
cacheRegion:String

The named query cache region from which to evict.

Exceptions
HibernateException:
Since a not-found 'cacheRegion' simply no-ops, this should indicate a problem communicating with underlying cache impl.
evictQueriesback to summary
public void evictQueries() throws HibernateException

Deprecated

Use Cache#evictQueryRegions accessed through getCache() instead.

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

Exceptions
HibernateException:
Indicate a problem communicating with underlying cache impl.
getAllClassMetadataback to summary
public Map<String, ClassMetadata> getAllClassMetadata()

Retrieve the ClassMetadata for all mapped entities.

Returns:Map<String, ClassMetadata>

A map containing all ClassMetadata keyed by the corresponding String entity-name.

Exceptions
HibernateException:
Generally empty map is returned instead of throwing.
Since
3.0 changed key from Class to String.
getAllCollectionMetadataback to summary
public Map<K, V> getAllCollectionMetadata()

Get the CollectionMetadata for all mapped collections

Returns:Map<K, V>

a map from String to CollectionMetadata

Exceptions
HibernateException:
Generally empty map is returned instead of throwing.
getCacheback to summary
public Cache getCache()

Obtain direct access to the underlying cache regions.

Returns:Cache

The direct cache access API.

getClassMetadataback to summary
public ClassMetadata getClassMetadata(Class<T> entityClass)

Retrieve the ClassMetadata associated with the given entity class.

Parameters
entityClass:Class<T>

The entity class

Returns:ClassMetadata

The metadata associated with the given entity; may be null if no such entity was mapped.

Exceptions
HibernateException:
Generally null is returned instead of throwing.
getClassMetadataback to summary
public ClassMetadata getClassMetadata(String entityName)

Retrieve the ClassMetadata associated with the given entity class.

Parameters
entityName:String

The entity class

Returns:ClassMetadata

The metadata associated with the given entity; may be null if no such entity was mapped.

Exceptions
HibernateException:
Generally null is returned instead of throwing.
Since
3.0
getCollectionMetadataback to summary
public CollectionMetadata getCollectionMetadata(String roleName)

Get the CollectionMetadata associated with the named collection role.

Parameters
roleName:String

The collection role (in form [owning-entity-name].[collection-property-name]).

Returns:CollectionMetadata

The metadata associated with the given collection; may be null if no such collection was mapped.

Exceptions
HibernateException:
Generally null is returned instead of throwing.
getCurrentSessionback to summary
public Session getCurrentSession() throws HibernateException

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

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

Returns:Session

The current session.

Exceptions
HibernateException:
Indicates an issue locating a suitable current session.
getDefinedFilterNamesback to summary
public Set<E> getDefinedFilterNames()

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

Returns:Set<E>

The set of filter names.

getFilterDefinitionback to summary
public FilterDefinition getFilterDefinition(String filterName) throws HibernateException

Obtain the definition of a filter by name.

Parameters
filterName:String

The name of the filter for which to obtain the definition.

Returns:FilterDefinition

The filter definition.

Exceptions
HibernateException:
If no filter defined with the given name.
getStatisticsback to summary
public Statistics getStatistics()

Retrieve the statistics fopr this factory.

Returns:Statistics

The statistics.

getTypeHelperback to summary
public TypeHelper getTypeHelper()

Retrieve this factory's TypeHelper

Returns:TypeHelper

The factory's TypeHelper

isClosedback to summary
public boolean isClosed()

Is this factory already closed?

Returns:boolean

True if this factory is already closed; false otherwise.

openSessionback to summary
public Session openSession() throws HibernateException

Open a Session.

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

Returns:Session

The created session.

Exceptions
HibernateException:
Indicates a peroblem opening the session; pretty rare here.
openSessionback to summary
public Session openSession(Interceptor interceptor) throws HibernateException

Open a Session, utilizing the specified Interceptor.

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

Parameters
interceptor:Interceptor

a session-scoped interceptor

Returns:Session

The created session.

Exceptions
HibernateException:
Indicates a peroblem opening the session; pretty rare here.
openSessionback to summary
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 org.hibernate.connection.ConnectionProvider instead as a highly recommended alternative.

Parameters
connection:Connection

a connection provided by the application.

Returns:Session

The created session.

openSessionback to summary
public Session openSession(Connection connection, Interceptor interceptor)

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 org.hibernate.connection.ConnectionProvider instead as a highly recommended alternative.

Parameters
connection:Connection

a connection provided by the application.

interceptor:Interceptor

a session-scoped interceptor

Returns:Session

The created session.

openStatelessSessionback to summary
public StatelessSession openStatelessSession()

Open a new stateless session.

Returns:StatelessSession

The created stateless session.

openStatelessSessionback to summary
public StatelessSession openStatelessSession(Connection connection)

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

Parameters
connection:Connection

Connection provided by the application.

Returns:StatelessSession

The created stateless session.