org.hibernate.impl
Class SessionImpl

java.lang.Object
  extended byorg.hibernate.impl.AbstractSessionImpl
      extended byorg.hibernate.impl.SessionImpl
All Implemented Interfaces:
EventSource, JDBCContext.Context, Serializable, Session, Session, SessionImplementor, TransactionFactory.Context

public final class SessionImpl
extends AbstractSessionImpl
implements EventSource, Session, JDBCContext.Context

Concrete implementation of a Session, and also the central, organizing component of Hibernate's internal implementation. As such, this class exposes two interfaces; Session itself, to the application, and SessionImplementor, to other components of Hibernate. This class is not threadsafe.

Author:
Gavin King
See Also:
Serialized Form

Field Summary
 
Fields inherited from class org.hibernate.impl.AbstractSessionImpl
factory
 
Method Summary
 void afterOperation(boolean success)
          Check if there is a Hibernate or JTA transaction in progress and, if there is not, flush if necessary, make sure the connection has been committed (if it is not in autocommit mode) and run the after completion processing
 void afterScrollOperation()
           
 void afterTransactionBegin(Transaction tx)
          We cannot rely upon this method being called! It is only called if we are using Hibernate Transaction API.
 void afterTransactionCompletion(boolean success, Transaction tx)
          Notify the session that the transaction completed, so we no longer own the old locks.
protected  boolean autoFlushIfRequired(Set querySpaces)
          detect in-memory changes, determine if the changes are to tables named in the query and, if so, complete execution the flush
 void beforeTransactionCompletion(Transaction tx)
          Notify the session that the transaction is about to complete
 Transaction beginTransaction()
          Begin a unit of work and return the associated Transaction object.
 String bestGuessEntityName(Object object)
          The best guess entity name for an entity not in an association
 void cancelQuery()
          Cancel the execution of the current query.
 void clear()
          Completely clear the session.
 Connection close()
          End the session by releasing the JDBC connection and cleaning up.
 Connection connection()
          Get the JDBC connection of this Session.

If the session is using aggressive collection release (as in a CMT environment), it is the application's responsibility to close the connection returned by this call.
 boolean contains(Object object)
          Check if this instance is associated with this Session.
 Criteria createCriteria(Class persistentClass)
          Create a new Criteria instance, for the given entity class, or a superclass of an entity class.
 Criteria createCriteria(Class persistentClass, String alias)
          Create a new Criteria instance, for the given entity class, or a superclass of an entity class, with the given alias.
 Criteria createCriteria(String entityName)
          Create a new Criteria instance, for the given entity name.
 Criteria createCriteria(String entityName, String alias)
          Create a new Criteria instance, for the given entity name, with the given alias.
 Query createFilter(Object collection, String queryString)
          Create a new instance of Query for the given collection and filter string.
 Query createQuery(String queryString)
          Create a new instance of Query for the given HQL query string.
 SQLQuery createSQLQuery(String sql)
          Create a new instance of SQLQuery for the given SQL query string.
 Query createSQLQuery(String sql, String[] returnAliases, Class[] returnClasses)
          Create a new instance of Query for the given SQL string.
 Query createSQLQuery(String sql, String returnAlias, Class returnClass)
          Create a new instance of Query for the given SQL string.
 void delete(Object object)
          Delete a persistent object
 int delete(String query)
          Delete all objects returned by the query.
 void delete(String entityName, Object object)
          Delete a persistent object (by explicit entity name)
 int delete(String query, Object[] values, Type[] types)
          Delete all objects returned by the query.
 void delete(String entityName, Object object, boolean isCascadeDeleteEnabled, Set transientEntities)
          Delete a persistent object
 int delete(String query, Object value, Type type)
          Delete all objects returned by the query.
 void disableFilter(String filterName)
          Disable the named filter for the current session.
 Connection disconnect()
          Disconnect the Session from the current JDBC connection.
 Filter enableFilter(String filterName)
          Enable the named filter for this current session.
 void evict(Object object)
          remove any hard references to the entity that are held by the infrastructure (references held by application or other persistant instances are okay)
 int executeNativeUpdate(NativeSQLQuerySpecification nativeQuerySpecification, QueryParameters queryParameters)
          Execute a native SQL update or delete query
 int executeUpdate(String query, QueryParameters queryParameters)
          Execute a HQL update or delete query
 Collection filter(Object collection, String filter)
          Apply a filter to a persistent collection.
 Collection filter(Object collection, String filter, Object[] values, Type[] types)
          Apply a filter to a persistent collection.
 Collection filter(Object collection, String filter, Object value, Type type)
          Apply a filter to a persistent collection.
 List find(String query)
          Retrieve a list of persistent objects using a hibernate query
 List find(String query, Object[] values, Type[] types)
          Execute a query with bind parameters, binding an array of values to "?" parameters in the query string.
 List find(String query, Object value, Type type)
          Execute a query with bind parameters, binding a value to a "?" parameter in the query string.
 void flush()
          Force this session to flush.
 void forceFlush(EntityEntry entityEntry)
          Force an immediate flush
 Object get(Class entityClass, Serializable id)
          Return the persistent instance of the given entity class with the given identifier, or null if there is no such persistent instance.
 Object get(Class entityClass, Serializable id, LockMode lockMode)
          Return the persistent instance of the given entity class with the given identifier, or null if there is no such persistent instance.
 Object get(String entityName, Serializable id)
          Return the persistent instance of the given named entity with the given identifier, or null if there is no such persistent instance.
 Object get(String entityName, Serializable id, LockMode lockMode)
          Return the persistent instance of the given entity class with the given identifier, or null if there is no such persistent instance.
 ActionQueue getActionQueue()
          Get the ActionQueue for this session
 Batcher getBatcher()
          Get the prepared statement Batcher for this session
 CacheMode getCacheMode()
          Get the current cache mode.
 ConnectionReleaseMode getConnectionReleaseMode()
           
 Serializable getContextEntityIdentifier(Object object)
          Get the id value for an object that is actually associated with the session.
 LockMode getCurrentLockMode(Object object)
          Determine the current lock mode of the given object.
 int getDontFlushFromFind()
           
 Filter getEnabledFilter(String filterName)
          Retrieve a currently enabled filter by name.
 Map getEnabledFilters()
          Return the currently enabled filters.
 EntityMode getEntityMode()
          Retrieve the entity mode in effect for this session.
 String getEntityName(Object object)
          Return the entity name for a persistent entity
 EntityPersister getEntityPersister(String entityName, Object object)
          Get the EntityPersister for any instance
 Object getEntityUsingInterceptor(EntityKey key)
          Get the entity instance associated with the given Key, calling the Interceptor if necessary
 String getFetchProfile()
           
 Type getFilterParameterType(String filterParameterName)
          Retreive the type for a given filter parrameter.
 Object getFilterParameterValue(String filterParameterName)
          Retreive the currently set value for a filter parameter.
 FlushMode getFlushMode()
          Get the current flush mode for this session.
 Serializable getIdentifier(Object object)
          Return the identifier value of the given entity as associated with this session.
 Interceptor getInterceptor()
          Retrieves the interceptor currently in use by this event source.
 JDBCContext getJDBCContext()
           
 EventListeners getListeners()
          Retrieves the configured event listeners from this event source.
 Query getNamedQuery(String queryName)
          Get a Query instance for a named query or named native SQL query
 PersistenceContext getPersistenceContext()
          Get the persistence context for this session
 Session getSession(EntityMode entityMode)
          Starts a new Session with the given entity mode in effect.
 SessionFactory getSessionFactory()
          Get the session factory which created this session.
 SessionStatistics getStatistics()
          Get the statistics for this session.
 long getTimestamp()
          System time before the start of the transaction
 Transaction getTransaction()
          Get the Transaction instance associated with this session.
 String guessEntityName(Object object)
          The guessed entity name for an entity not in an association
 Object immediateLoad(String entityName, Serializable id)
          Load the data for the object with the specified id into a newly created object.
 void initializeCollection(PersistentCollection collection, boolean writing)
          Initialize the collection (if not already initialized)
 Object instantiate(EntityPersister persister, Serializable id)
          give the interceptor an opportunity to override the default instantiation
 Object instantiate(String entityName, Serializable id)
          Instantiate the entity class, initializing with the given identifier
 Object internalLoad(String entityName, Serializable id, boolean eager, boolean nullable)
          Load an instance without checking if it was deleted.
 boolean isAutoCloseSessionEnabled()
           
 boolean isConnected()
          Check if the session is currently connected.
 boolean isDirty()
          Does this session contain any changes which must be synchronized with the database? In other words, would any DML operations be executed if we flushed this session?
 boolean isEventSource()
           
 boolean isFlushBeforeCompletionEnabled()
           
 boolean isFlushModeNever()
           
 boolean isOpen()
          Check if the session is still open.
 boolean isTransactionInProgress()
          Does this Session have an active Hibernate transaction or is there a JTA transaction in progress?
 Iterator iterate(String query)
          Execute a query and return the results in an iterator.
 Iterator iterate(String query, Object[] values, Type[] types)
          Execute a query and return the results in an iterator.
 Iterator iterate(String query, Object value, Type type)
          Execute a query and return the results in an iterator.
 Iterator iterate(String query, QueryParameters queryParameters)
          Execute an iterate() query
 Iterator iterateFilter(Object collection, String filter, QueryParameters queryParameters)
          Iterate a filter
 List list(CriteriaImpl criteria)
          Execute a criteria query
 List list(String query, QueryParameters queryParameters)
          Execute a find() query
 List listCustomQuery(CustomQuery customQuery, QueryParameters queryParameters)
          Execute an SQL Query
 List listFilter(Object collection, String filter, QueryParameters queryParameters)
          Execute a filter
 Object load(Class entityClass, Serializable id)
          Return the persistent instance of the given entity class with the given identifier, assuming that the instance exists.
 Object load(Class entityClass, Serializable id, LockMode lockMode)
          Return the persistent instance of the given entity class with the given identifier, obtaining the specified lock mode, assuming the instance exists.
 void load(Object object, Serializable id)
          Read the persistent state associated with the given identifier into the given transient instance.
 Object load(String entityName, Serializable id)
          Return the persistent instance of the given entity class with the given identifier, assuming that the instance exists.
 Object load(String entityName, Serializable id, LockMode lockMode)
          Return the persistent instance of the given entity class with the given identifier, obtaining the specified lock mode, assuming the instance exists.
 void lock(Object object, LockMode lockMode)
          Obtain the specified lock level upon the given object.
 void lock(String entityName, Object object, LockMode lockMode)
          Obtain the specified lock level upon the given object.
 void managedClose()
           
 void managedFlush()
           
 Object merge(Object object)
          Copy the state of the given object onto the persistent object with the same identifier.
 Object merge(String entityName, Object object)
          Copy the state of the given object onto the persistent object with the same identifier.
 void merge(String entityName, Object object, Map copiedAlready)
          Cascade merge an entity instance
 void persist(Object object)
          Make a transient instance persistent.
 void persist(String entityName, Object object)
          Make a transient instance persistent.
 void persist(String entityName, Object object, Map copiedAlready)
          Cascade persist an entity instance
 void persistOnFlush(Object object)
           
 void persistOnFlush(String entityName, Object object)
           
 void persistOnFlush(String entityName, Object object, Map copiedAlready)
          Cascade persist an entity instance during the flush process
 void reconnect()
          Obtain a new JDBC connection.
 void reconnect(Connection conn)
          Reconnect to the given JDBC connection.
 void refresh(Object object)
          Re-read the state of the given instance from the underlying database.
 void refresh(Object object, LockMode lockMode)
          Re-read the state of the given instance from the underlying database, with the given LockMode.
 void refresh(Object object, Map refreshedAlready)
          Cascade refesh an entity instance
 void replicate(Object obj, ReplicationMode replicationMode)
          Persist the state of the given detached instance, reusing the current identifier value.
 void replicate(String entityName, Object obj, ReplicationMode replicationMode)
          Persist the state of the given detached instance, reusing the current identifier value.
 Serializable save(Object obj)
          Persist the given transient instance, first assigning a generated identifier.
 void save(Object obj, Serializable id)
          Persist the given transient instance, using the given identifier.
 Serializable save(String entityName, Object object)
          Persist the given transient instance, first assigning a generated identifier.
 void save(String entityName, Object object, Serializable id)
          Persist the given transient instance, using the given identifier.
 void saveOrUpdate(Object object)
          Either save(Object) or update(Object) the given instance, depending upon resolution of the unsaved-value checks (see the manual for discussion of unsaved-value checking).
 void saveOrUpdate(String entityName, Object obj)
          Either save(String, Object) or update(String, Object) the given instance, depending upon resolution of the unsaved-value checks (see the manual for discussion of unsaved-value checking).
 Object saveOrUpdateCopy(Object object)
          Copy the state of the given object onto the persistent object with the same identifier.
 Object saveOrUpdateCopy(Object object, Serializable id)
          Copy the state of the given object onto the persistent object with the given identifier.
 Object saveOrUpdateCopy(String entityName, Object object)
          Copy the state of the given object onto the persistent object with the same identifier.
 void saveOrUpdateCopy(String entityName, Object object, Map copiedAlready)
          Cascade copy an entity instance
 Object saveOrUpdateCopy(String entityName, Object object, Serializable id)
          Copy the state of the given object onto the persistent object with the given identifier.
 ScrollableResults scroll(CriteriaImpl criteria, ScrollMode scrollMode)
          Execute a criteria query
 ScrollableResults scroll(String query, QueryParameters queryParameters)
          Execute a scroll() query
 ScrollableResults scrollCustomQuery(CustomQuery customQuery, QueryParameters queryParameters)
          Execute an SQL Query
 void setAutoClear(boolean enabled)
          Enable/disable automatic cache clearing from after transaction completion (for EJB3)
 void setCacheMode(CacheMode cacheMode)
          Set the cache mode.
 void setFetchProfile(String fetchProfile)
           
 void setFlushMode(FlushMode flushMode)
          Set the flush mode for this session.
 void setReadOnly(Object entity, boolean readOnly)
          Set an unmodified persistent object to read only mode, or a read only object to modifiable mode.
 boolean shouldAutoClose()
           
 String toString()
           
 void update(Object obj)
          Update the persistent instance with the identifier of the given detached instance.
 void update(Object obj, Serializable id)
          Update the persistent state associated with the given identifier.
 void update(String entityName, Object object)
          Update the persistent instance with the identifier of the given detached instance.
 void update(String entityName, Object object, Serializable id)
          Update the persistent state associated with the given identifier.
 
Methods inherited from class org.hibernate.impl.AbstractSessionImpl
errorIfClosed, getFactory, getHQLQueryPlan, getNamedSQLQuery, getNativeSQLQueryPlan, isClosed, list, scroll, setClosed
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface org.hibernate.engine.SessionImplementor
getFactory, getNamedSQLQuery, isClosed, list, scroll
 
Methods inherited from interface org.hibernate.transaction.TransactionFactory.Context
getFactory, isClosed
 

Method Detail

getSession

public Session getSession(EntityMode entityMode)
Description copied from interface: Session
Starts a new Session with the given entity mode in effect. This secondary Session inherits the connection, transaction, and other context information from the primary Session. It doesn't need to be flushed or closed by the developer.

Specified by:
getSession in interface Session
Parameters:
entityMode - The entity mode to use for the new session.
Returns:
The new session

clear

public void clear()
Description copied from interface: Session
Completely clear the session. Evict all loaded instances and cancel all pending saves, updates and deletions. Do not close open iterators or instances of ScrollableResults.

Specified by:
clear in interface Session

getBatcher

public Batcher getBatcher()
Description copied from interface: SessionImplementor
Get the prepared statement Batcher for this session

Specified by:
getBatcher in interface SessionImplementor

getTimestamp

public long getTimestamp()
Description copied from interface: SessionImplementor
System time before the start of the transaction

Specified by:
getTimestamp in interface SessionImplementor

close

public Connection close()
                 throws HibernateException
Description copied from interface: Session
End the session by releasing the JDBC connection and cleaning up. It is not strictly necessary to close the session but you must at least Session.disconnect() it.

Specified by:
close in interface Session
Returns:
the connection provided by the application or null.
Throws:
HibernateException - Indicates problems cleaning up.

getConnectionReleaseMode

public ConnectionReleaseMode getConnectionReleaseMode()
Specified by:
getConnectionReleaseMode in interface JDBCContext.Context

isAutoCloseSessionEnabled

public boolean isAutoCloseSessionEnabled()
Specified by:
isAutoCloseSessionEnabled in interface JDBCContext.Context

isOpen

public boolean isOpen()
Description copied from interface: Session
Check if the session is still open.

Specified by:
isOpen in interface SessionImplementor

isFlushModeNever

public boolean isFlushModeNever()
Specified by:
isFlushModeNever in interface TransactionFactory.Context

isFlushBeforeCompletionEnabled

public boolean isFlushBeforeCompletionEnabled()
Specified by:
isFlushBeforeCompletionEnabled in interface TransactionFactory.Context

managedFlush

public void managedFlush()
Specified by:
managedFlush in interface TransactionFactory.Context

shouldAutoClose

public boolean shouldAutoClose()
Specified by:
shouldAutoClose in interface TransactionFactory.Context

managedClose

public void managedClose()
Specified by:
managedClose in interface TransactionFactory.Context

connection

public Connection connection()
                      throws HibernateException
Description copied from interface: Session
Get the JDBC connection of this Session.

If the session is using aggressive collection release (as in a CMT environment), it is the application's responsibility to close the connection returned by this call. Otherwise, the application should not close the connection.

Specified by:
connection in interface SessionImplementor
Throws:
HibernateException

isConnected

public boolean isConnected()
Description copied from interface: Session
Check if the session is currently connected.

Specified by:
isConnected in interface SessionImplementor

isTransactionInProgress

public boolean isTransactionInProgress()
Description copied from interface: SessionImplementor
Does this Session have an active Hibernate transaction or is there a JTA transaction in progress?

Specified by:
isTransactionInProgress in interface SessionImplementor

disconnect

public Connection disconnect()
                      throws HibernateException
Description copied from interface: Session
Disconnect the Session from the current JDBC connection. If the connection was obtained by Hibernate close it and return it to the connection pool; otherwise, return it to the application.

This is used by applications which supply JDBC connections to Hibernate and which require long-sessions (or long-conversations)

Note that disconnect() called on a session where the connection was retrieved by Hibernate through its configured ConnectionProvider has no effect, provided ConnectionReleaseMode.ON_CLOSE is not in effect.

Specified by:
disconnect in interface Session
Returns:
the application-supplied connection or null
Throws:
HibernateException
See Also:
Session.reconnect(Connection), Session.reconnect()

reconnect

public void reconnect()
               throws HibernateException
Description copied from interface: Session
Obtain a new JDBC connection. This is used by applications which require long transactions and do not supply connections to the session.

Specified by:
reconnect in interface Session
Throws:
HibernateException
See Also:
Session.disconnect()

reconnect

public void reconnect(Connection conn)
               throws HibernateException
Description copied from interface: Session
Reconnect to the given JDBC connection. This is used by applications which require long transactions and use application-supplied connections.

Specified by:
reconnect in interface Session
Parameters:
conn - a JDBC connection
Throws:
HibernateException
See Also:
Session.disconnect()

beforeTransactionCompletion

public void beforeTransactionCompletion(Transaction tx)
Description copied from interface: SessionImplementor
Notify the session that the transaction is about to complete

Specified by:
beforeTransactionCompletion in interface SessionImplementor

setAutoClear

public void setAutoClear(boolean enabled)
Description copied from interface: SessionImplementor
Enable/disable automatic cache clearing from after transaction completion (for EJB3)

Specified by:
setAutoClear in interface