public interface Session extends Serializable
Session sess = factory.openSession(); Transaction tx; try { tx = sess.beginTransaction(); //do some work ... tx.commit(); } catch (Exception e) { if (tx!=null) tx.rollback(); throw e; } finally { sess.close(); }
SessionFactory
Modifier and Type | Interface and Description |
---|---|
static interface |
Session.LockRequest
Contains locking details (LockMode, Timeout and Scope).
|
Modifier and Type | Method and Description |
---|---|
Transaction |
beginTransaction()
Begin a unit of work and return the associated Transaction object.
|
Session.LockRequest |
buildLockRequest(LockOptions lockOptions)
Build a LockRequest that specifies the LockMode, pessimistic lock timeout and lock scope.
|
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()
Deprecated.
(scheduled for removal in 4.x). Replacement depends on need; for doing direct JDBC stuff use
doWork(org.hibernate.jdbc.Work) ; for opening a 'temporary Session' use (TBD). |
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 queryString)
Create a new instance of SQLQuery for the given SQL query string.
|
void |
delete(Object object)
Remove a persistent instance from the datastore.
|
void |
delete(String entityName,
Object object)
Remove a persistent instance from the datastore.
|
void |
disableFetchProfile(String name)
Disable a particular fetch profile on this session.
|
void |
disableFilter(String filterName)
Disable the named filter for the current session.
|
Connection |
disconnect()
Disconnect the Session from the current JDBC connection.
|
void |
doWork(Work work)
Controller for allowing users to perform JDBC related work using the Connection
managed by this Session.
|
void |
enableFetchProfile(String name)
Enable a particular fetch profile on this session.
|
Filter |
enableFilter(String filterName)
Enable the named filter for this current session.
|
void |
evict(Object object)
Remove this instance from the session cache.
|
void |
flush()
Force this session to flush.
|
Object |
get(Class clazz,
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 clazz,
Serializable id,
LockMode lockMode)
Deprecated.
LockMode parameter should be replaced with LockOptions
|
Object |
get(Class clazz,
Serializable id,
LockOptions lockOptions)
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)
Deprecated.
LockMode parameter should be replaced with LockOptions
|
Object |
get(String entityName,
Serializable id,
LockOptions lockOptions)
Return the persistent instance of the given entity class with the given identifier,
or null if there is no such persistent instance.
|
CacheMode |
getCacheMode()
Get the current cache mode.
|
LockMode |
getCurrentLockMode(Object object)
Determine the current lock mode of the given object.
|
Filter |
getEnabledFilter(String filterName)
Retrieve a currently enabled filter by name.
|
EntityMode |
getEntityMode()
Retrieve the entity mode in effect for this session.
|
String |
getEntityName(Object object)
Return the entity name for a persistent entity
|
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.
|
LobHelper |
getLobHelper()
Retrieve this session's helper/delegate for creating LOB instances.
|
Query |
getNamedQuery(String queryName)
Obtain an instance of Query for a named query string defined in the
mapping file.
|
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.
|
Transaction |
getTransaction()
Get the Transaction instance associated with this session.
|
TypeHelper |
getTypeHelper()
Convenience access to the
TypeHelper associated with this session's SessionFactory . |
boolean |
isConnected()
Check if the session is currently connected.
|
boolean |
isDefaultReadOnly()
Will entities and proxies that are loaded into this session be made
read-only by default?
To determine the read-only/modifiable setting for a particular entity
or proxy:
|
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 |
isFetchProfileEnabled(String name)
Is a particular fetch profile enabled on this session?
|
boolean |
isOpen()
Check if the session is still open.
|
boolean |
isReadOnly(Object entityOrProxy)
Is the specified entity or proxy read-only?
To get the default read-only/modifiable setting used for
entities and proxies that are loaded into the session:
|
Object |
load(Class theClass,
Serializable id)
Return the persistent instance of the given entity class with the given identifier,
assuming that the instance exists.
|
Object |
load(Class theClass,
Serializable id,
LockMode lockMode)
Deprecated.
LockMode parameter should be replaced with LockOptions
|
Object |
load(Class theClass,
Serializable id,
LockOptions lockOptions)
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)
Deprecated.
LockMode parameter should be replaced with LockOptions
|
Object |
load(String entityName,
Serializable id,
LockOptions lockOptions)
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)
Deprecated.
instead call buildLockRequest(LockMode).lock(object)
|
void |
lock(String entityName,
Object object,
LockMode lockMode)
Deprecated.
instead call buildLockRequest(LockMode).lock(entityName, object)
|
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 |
persist(Object object)
Make a transient instance persistent.
|
void |
persist(String entityName,
Object object)
Make a transient instance persistent.
|
void |
reconnect()
Deprecated.
Manual reconnection is only needed in the case of
application-supplied connections, in which case the
reconnect(java.sql.Connection) for should be used. |
void |
reconnect(Connection connection)
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)
Deprecated.
LockMode parameter should be replaced with LockOptions
|
void |
refresh(Object object,
LockOptions lockOptions)
Re-read the state of the given instance from the underlying database, with
the given LockMode.
|
void |
replicate(Object object,
ReplicationMode replicationMode)
Persist the state of the given detached instance, reusing the current
identifier value.
|
void |
replicate(String entityName,
Object object,
ReplicationMode replicationMode)
Persist the state of the given detached instance, reusing the current
identifier value.
|
Serializable |
save(Object object)
Persist the given transient instance, first assigning a generated identifier.
|
Serializable |
save(String entityName,
Object object)
Persist the given transient instance, first assigning a generated 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 object)
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). |
void |
setCacheMode(CacheMode cacheMode)
Set the cache mode.
|
void |
setDefaultReadOnly(boolean readOnly)
Change the default for entities and proxies loaded into this session
from modifiable to read-only mode, or from modifiable to read-only mode.
|
void |
setFlushMode(FlushMode flushMode)
Set the flush mode for this session.
|
void |
setReadOnly(Object entityOrProxy,
boolean readOnly)
Set an unmodified persistent object to read-only mode, or a read-only
object to modifiable mode.
|
void |
update(Object object)
Update the persistent instance with the identifier of the given detached
instance.
|
void |
update(String entityName,
Object object)
Update the persistent instance with the identifier of the given detached
instance.
|
EntityMode getEntityMode()
Session getSession(EntityMode entityMode)
entityMode
- The entity mode to use for the new session.void flush() throws HibernateException
flush-mode
,
Transaction.commit()
calls this method).
Flushing is the process of synchronizing the underlying persistent
store with persistable state held in memory.HibernateException
- Indicates problems flushing the session or
talking to the database.void setFlushMode(FlushMode flushMode)
FlushMode.MANUAL
at the start of the session (in
order to achieve some extra performance).flushMode
- the new flush modeFlushMode
FlushMode getFlushMode()
void setCacheMode(CacheMode cacheMode)
cacheMode
- The new cache mode.CacheMode getCacheMode()
SessionFactory getSessionFactory()
SessionFactory
Connection connection() throws HibernateException
doWork(org.hibernate.jdbc.Work)
; for opening a 'temporary Session' use (TBD).HibernateException
- if the Session is disconnectedConnection close() throws HibernateException
disconnect()
it.HibernateException
- Indicates problems cleaning up.void cancelQuery() throws HibernateException
HibernateException
- There was a problem canceling the queryboolean isOpen()
boolean isConnected()
boolean isDirty() throws HibernateException
HibernateException
- could not perform dirtying checkingboolean isDefaultReadOnly()
isReadOnly(Object)
void setDefaultReadOnly(boolean readOnly)
readOnly
- true, the default for loaded entities/proxies is read-only;
false, the default for loaded entities/proxies is modifiableTo override this session's read-only/modifiable setting for entities
and proxies loaded by a Query:
,
Query.setReadOnly(boolean)
Serializable getIdentifier(Object object) throws HibernateException
object
- a persistent instanceTransientObjectException
- if the instance is transient or associated with
a different sessionHibernateException
boolean contains(Object object)
object
- an instance of a persistent classvoid evict(Object object) throws HibernateException
object
- a persistent instanceHibernateException
Object load(Class theClass, Serializable id, LockMode lockMode) throws HibernateException
theClass
- a persistent classid
- a valid identifier of an existing persistent instance of the classlockMode
- the lock levelHibernateException
Object load(Class theClass, Serializable id, LockOptions lockOptions) throws HibernateException
theClass
- a persistent classid
- a valid identifier of an existing persistent instance of the classlockOptions
- contains the lock levelHibernateException
Object load(String entityName, Serializable id, LockMode lockMode) throws HibernateException
entityName
- a persistent classid
- a valid identifier of an existing persistent instance of the classlockMode
- the lock levelHibernateException
Object load(String entityName, Serializable id, LockOptions lockOptions) throws HibernateException
entityName
- a persistent classid
- a valid identifier of an existing persistent instance of the classlockOptions
- contains the lock levelHibernateException
Object load(Class theClass, Serializable id) throws HibernateException
theClass
- a persistent classid
- a valid identifier of an existing persistent instance of the classHibernateException
Object load(String entityName, Serializable id) throws HibernateException
entityName
- a persistent classid
- a valid identifier of an existing persistent instance of the classHibernateException
void load(Object object, Serializable id) throws HibernateException
object
- an "empty" instance of the persistent classid
- a valid identifier of an existing persistent instance of the classHibernateException
void replicate(Object object, ReplicationMode replicationMode) throws HibernateException
object
- a detached instance of a persistent classHibernateException
void replicate(String entityName, Object object, ReplicationMode replicationMode) throws HibernateException
object
- a detached instance of a persistent classHibernateException
Serializable save(Object object) throws HibernateException
object
- a transient instance of a persistent classHibernateException
Serializable save(String entityName, Object object) throws HibernateException
object
- a transient instance of a persistent classHibernateException
void saveOrUpdate(Object object) throws HibernateException
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).
This operation cascades to associated instances if the association is mapped
with cascade="save-update".object
- a transient or detached instance containing new or updated stateHibernateException
save(java.lang.Object)
,
update(Object object)
void saveOrUpdate(String entityName, Object object) throws HibernateException
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).
This operation cascades to associated instances if the association is mapped
with cascade="save-update".object
- a transient or detached instance containing new or updated stateHibernateException
save(String,Object)
,
update(String,Object)
void update(Object object) throws HibernateException
object
- a detached instance containing updated stateHibernateException
void update(String entityName, Object object) throws HibernateException
object
- a detached instance containing updated stateHibernateException
Object merge(Object object) throws HibernateException
object
- a detached instance with state to be copiedHibernateException
Object merge(String entityName, Object object) throws HibernateException
object
- a detached instance with state to be copiedHibernateException
void persist(Object object) throws HibernateException
object
- a transient instance to be made persistentHibernateException
void persist(String entityName, Object object) throws HibernateException
object
- a transient instance to be made persistentHibernateException
void delete(Object object) throws HibernateException
object
- the instance to be removedHibernateException
void delete(String entityName, Object object) throws HibernateException
entityName
- The entity name for the instance to be removed.object
- the instance to be removedHibernateException
void lock(Object object, LockMode lockMode) throws HibernateException
object
- a persistent or transient instancelockMode
- the lock levelHibernateException
void lock(String entityName, Object object, LockMode lockMode) throws HibernateException
object
- a persistent or transient instancelockMode
- the lock levelHibernateException
Session.LockRequest buildLockRequest(LockOptions lockOptions)
lockOptions
- contains the lock levelHibernateException
void refresh(Object object) throws HibernateException
object
- a persistent or detached instanceHibernateException
void refresh(Object object, LockMode lockMode) throws HibernateException
object
- a persistent or detached instancelockMode
- the lock mode to useHibernateException
void refresh(Object object, LockOptions lockOptions) throws HibernateException
object
- a persistent or detached instancelockOptions
- contains the lock mode to useHibernateException
LockMode getCurrentLockMode(Object object) throws HibernateException
object
- a persistent instanceHibernateException
Transaction beginTransaction() throws HibernateException
HibernateException
Transaction
Transaction getTransaction()
HibernateException
Transaction
Criteria createCriteria(Class persistentClass)
persistentClass
- a class, which is persistent, or has persistent subclassesCriteria createCriteria(Class persistentClass, String alias)
persistentClass
- a class, which is persistent, or has persistent subclassesCriteria createCriteria(String entityName)
entityName
- Criteria createCriteria(String entityName, String alias)
entityName
- Query createQuery(String queryString) throws HibernateException
queryString
- a HQL queryHibernateException
SQLQuery createSQLQuery(String queryString) throws HibernateException
queryString
- a SQL queryHibernateException
Query createFilter(Object collection, String queryString) throws HibernateException
collection
- a persistent collectionqueryString
- a Hibernate queryHibernateException
Query getNamedQuery(String queryName) throws HibernateException
queryName
- the name of a query defined externallyHibernateException
void clear()
Object get(Class clazz, Serializable id) throws HibernateException
clazz
- a persistent classid
- an identifierHibernateException
Object get(Class clazz, Serializable id, LockMode lockMode) throws HibernateException
clazz
- a persistent classid
- an identifierlockMode
- the lock modeHibernateException
Object get(Class clazz, Serializable id, LockOptions lockOptions) throws HibernateException
clazz
- a persistent classid
- an identifierlockOptions
- the lock modeHibernateException
Object get(String entityName, Serializable id) throws HibernateException
entityName
- the entity nameid
- an identifierHibernateException
Object get(String entityName, Serializable id, LockMode lockMode) throws HibernateException
entityName
- the entity nameid
- an identifierlockMode
- the lock modeHibernateException
Object get(String entityName, Serializable id, LockOptions lockOptions) throws HibernateException
entityName
- the entity nameid
- an identifierlockOptions
- contains the lock modeHibernateException
String getEntityName(Object object) throws HibernateException
object
- a persistent entityHibernateException
Filter enableFilter(String filterName)
filterName
- The name of the filter to be enabled.Filter getEnabledFilter(String filterName)
filterName
- The name of the filter to be retrieved.void disableFilter(String filterName)
filterName
- The name of the filter to be disabled.SessionStatistics getStatistics()
boolean isReadOnly(Object entityOrProxy)
entityOrProxy
- an entity or HibernateProxyisDefaultReadOnly()
void setReadOnly(Object entityOrProxy, boolean readOnly)
entityOrProxy
- an entity or HibernateProxyreadOnly
- if true, the entity or proxy is made read-only;
if false, the entity or proxy is made modifiable.To override this session's read-only/modifiable setting for entities
and proxies loaded by a Query:
,
Query.setReadOnly(boolean)
void doWork(Work work) throws HibernateException
work
- The work to be performed.HibernateException
- Generally indicates wrapped SQLException
Connection disconnect() throws HibernateException
ConnectionProvider
has no effect,
provided ConnectionReleaseMode.ON_CLOSE
is not in effect.HibernateException
reconnect(Connection)
,
reconnect()
void reconnect() throws HibernateException
reconnect(java.sql.Connection)
for should be used.HibernateException
disconnect()
void reconnect(Connection connection) throws HibernateException
connection
- a JDBC connectionHibernateException
disconnect()
boolean isFetchProfileEnabled(String name) throws UnknownProfileException
name
- The name of the profile to be checked.UnknownProfileException
- Indicates that the given name does not
match any known profile namesfor discussion of this feature
void enableFetchProfile(String name) throws UnknownProfileException
name
- The name of the fetch profile to be enabled.UnknownProfileException
- Indicates that the given name does not
match any known profile namesfor discussion of this feature
void disableFetchProfile(String name) throws UnknownProfileException
name
- The name of the fetch profile to be disabled.UnknownProfileException
- Indicates that the given name does not
match any known profile namesfor discussion of this feature
TypeHelper getTypeHelper()
TypeHelper
associated with this session's SessionFactory
.
Equivalent to calling getSessionFactory()
.getTypeHelper()
TypeHelper
associated with this session's SessionFactory
LobHelper getLobHelper()
Copyright © 2002-2017 Red Hat Middleware, LLC. All Rights Reserved