|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
public interface Session
The main runtime interface between a Java application and Hibernate. This is the
central API class abstracting the notion of a persistence service.
The lifecycle of a Session is bounded by the beginning and end of a logical
transaction. (Long transactions might span several database transactions.)
The main function of the Session is to offer create, read and delete operations
for instances of mapped entity classes. Instances may exist in one of three states:
transient: never persistent, not associated with any Session
persistent: associated with a unique Session
detached: previously persistent, not associated with any Session
Transient instances may be made persistent by calling save(),
persist() or saveOrUpdate(). Persistent instances may be made transient
by calling delete(). Any instance returned by a get() or
load() method is persistent. Detached instances may be made persistent
by calling update(), saveOrUpdate(), lock() or replicate().
The state of a transient or detached instance may also be made persistent as a new
persistent instance by calling merge().
save() and persist() result in an SQL INSERT, delete()
in an SQL DELETE and update() or merge() in an SQL UPDATE.
Changes to persistent instances are detected at flush time and also result in an SQL
UPDATE. saveOrUpdate() and replicate() result in either an
INSERT or an UPDATE.
It is not intended that implementors be threadsafe. Instead each thread/transaction
should obtain its own instance from a SessionFactory.
A Session instance is serializable if its persistent classes are serializable.
A typical transaction should use the following idiom:
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
Nested Class Summary | |
---|---|
static interface |
Session.LockRequest
Contains locking details (LockMode, Timeout and Scope). |
Method Summary | ||
---|---|---|
Session.LockRequest |
buildLockRequest(LockOptions lockOptions)
Build a LockRequest that specifies the LockMode, pessimistic lock timeout and lock scope. |
|
IdentifierLoadAccess |
byId(Class entityClass)
Create an IdentifierLoadAccess instance to retrieve the specified entity by
primary key. |
|
IdentifierLoadAccess |
byId(String entityName)
Create an IdentifierLoadAccess instance to retrieve the specified entity type by
primary key. |
|
NaturalIdLoadAccess |
byNaturalId(Class entityClass)
Create an NaturalIdLoadAccess instance to retrieve the specified entity by
its natural id. |
|
NaturalIdLoadAccess |
byNaturalId(String entityName)
Create an NaturalIdLoadAccess instance to retrieve the specified entity by
its natural id. |
|
SimpleNaturalIdLoadAccess |
bySimpleNaturalId(Class entityClass)
Create an SimpleNaturalIdLoadAccess instance to retrieve the specified entity by
its simple (single attribute) natural id. |
|
SimpleNaturalIdLoadAccess |
bySimpleNaturalId(String entityName)
Create an SimpleNaturalIdLoadAccess instance to retrieve the specified entity by
its natural id. |
|
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. |
|
boolean |
contains(Object object)
Check if this instance is associated with this Session. |
|
Query |
createFilter(Object collection,
String queryString)
Create a Query instance for the given collection and filter 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 its underlying JDBC connection. |
|
|
doReturningWork(ReturningWork<T> work)
Controller for allowing users to perform JDBC related work using the Connection managed by this Session. |
|
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. |
|
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. |
|
SessionFactory |
getSessionFactory()
Get the session factory which created this session. |
|
SessionStatistics |
getStatistics()
Get the statistics for 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(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 |
refresh(String entityName,
Object object)
Re-read the state of the given instance from the underlying database. |
|
void |
refresh(String entityName,
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). |
|
SharedSessionBuilder |
sessionWithOptions()
Obtain a Session builder with the ability to grab certain information from this session. |
|
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. |
Methods inherited from interface org.hibernate.SharedSessionContract |
---|
beginTransaction, createCriteria, createCriteria, createCriteria, createCriteria, createQuery, createSQLQuery, getNamedQuery, getTenantIdentifier, getTransaction |
Method Detail |
---|
SharedSessionBuilder sessionWithOptions()
Session
builder with the ability to grab certain information from this session.
void flush() throws HibernateException
setFlushMode(FlushMode)
,
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 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)
object
- a persistent instance
TransientObjectException
- if the instance is transient or associated with
a different sessionboolean contains(Object object)
object
- an instance of a persistent class
void evict(Object object)
object
- a persistent instance@Deprecated Object load(Class theClass, Serializable id, LockMode lockMode)
theClass
- a persistent classid
- a valid identifier of an existing persistent instance of the classlockMode
- the lock level
Object load(Class theClass, Serializable id, LockOptions lockOptions)
theClass
- a persistent classid
- a valid identifier of an existing persistent instance of the classlockOptions
- contains the lock level
@Deprecated Object load(String entityName, Serializable id, LockMode lockMode)
entityName
- a persistent classid
- a valid identifier of an existing persistent instance of the classlockMode
- the lock level
Object load(String entityName, Serializable id, LockOptions lockOptions)
entityName
- a persistent classid
- a valid identifier of an existing persistent instance of the classlockOptions
- contains the lock level
Object load(Class theClass, Serializable id)
theClass
- a persistent classid
- a valid identifier of an existing persistent instance of the class
Object load(String entityName, Serializable id)
entityName
- a persistent classid
- a valid identifier of an existing persistent instance of the class
void load(Object object, Serializable id)
object
- an "empty" instance of the persistent classid
- a valid identifier of an existing persistent instance of the classvoid replicate(Object object, ReplicationMode replicationMode)
cascade="replicate"
object
- a detached instance of a persistent classreplicationMode
- The replication mode to usevoid replicate(String entityName, Object object, ReplicationMode replicationMode)
cascade="replicate"
entityName
- The entity nameobject
- a detached instance of a persistent classreplicationMode
- The replication mode to useSerializable save(Object object)
cascade="save-update"
object
- a transient instance of a persistent class
Serializable save(String entityName, Object object)
cascade="save-update"
entityName
- The entity nameobject
- a transient instance of a persistent class
void saveOrUpdate(Object object)
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 statesave(java.lang.Object)
,
update(Object object)
void saveOrUpdate(String entityName, Object object)
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"
entityName
- The entity nameobject
- a transient or detached instance containing new or updated statesave(String,Object)
,
update(String,Object)
void update(Object object)
cascade="save-update"
object
- a detached instance containing updated statevoid update(String entityName, Object object)
cascade="save-update"
entityName
- The entity nameobject
- a detached instance containing updated stateObject merge(Object object)
cascade="merge"
The semantics of this method are defined by JSR-220.
object
- a detached instance with state to be copied
Object merge(String entityName, Object object)
cascade="merge"
The semantics of this method are defined by JSR-220.
entityName
- The entity nameobject
- a detached instance with state to be copied
void persist(Object object)
cascade="persist"
The semantics of this method are defined by JSR-220.
object
- a transient instance to be made persistentvoid persist(String entityName, Object object)
cascade="persist"
The semantics of this method are defined by JSR-220.
entityName
- The entity nameobject
- a transient instance to be made persistentvoid delete(Object object)
cascade="delete"
object
- the instance to be removedvoid delete(String entityName, Object object)
cascade="delete"
entityName
- The entity name for the instance to be removed.object
- the instance to be removed@Deprecated void lock(Object object, LockMode lockMode)
object
- a persistent or transient instancelockMode
- the lock level@Deprecated void lock(String entityName, Object object, LockMode lockMode)
object
- a persistent or transient instancelockMode
- the lock levelSession.LockRequest buildLockRequest(LockOptions lockOptions)
session.buildLockRequest().setLockMode(LockMode.PESSIMISTIC_WRITE).setTimeOut(60000).lock(entity);
lockOptions
- contains the lock level
void refresh(Object object)
object
- a persistent or detached instancevoid refresh(String entityName, Object object)
entityName
- a persistent classobject
- a persistent or detached instance@Deprecated void refresh(Object object, LockMode lockMode)
object
- a persistent or detached instancelockMode
- the lock mode to usevoid refresh(Object object, LockOptions lockOptions)
object
- a persistent or detached instancelockOptions
- contains the lock mode to usevoid refresh(String entityName, Object object, LockOptions lockOptions)
entityName
- a persistent classobject
- a persistent or detached instancelockOptions
- contains the lock mode to useLockMode getCurrentLockMode(Object object)
object
- a persistent instance
Query createFilter(Object collection, String queryString)
Query
instance for the given collection and filter string. Contains an implicit FROM
element named this
which refers to the defined table for the collection elements, as well as an implicit
WHERE
restriction for this particular collection instance's key value.
collection
- a persistent collectionqueryString
- a Hibernate query fragment.
void clear()
Object get(Class clazz, Serializable id)
clazz
- a persistent classid
- an identifier
@Deprecated Object get(Class clazz, Serializable id, LockMode lockMode)
clazz
- a persistent classid
- an identifierlockMode
- the lock mode
Object get(Class clazz, Serializable id, LockOptions lockOptions)
clazz
- a persistent classid
- an identifierlockOptions
- the lock mode
Object get(String entityName, Serializable id)
entityName
- the entity nameid
- an identifier
@Deprecated Object get(String entityName, Serializable id, LockMode lockMode)
entityName
- the entity nameid
- an identifierlockMode
- the lock mode
Object get(String entityName, Serializable id, LockOptions lockOptions)
entityName
- the entity nameid
- an identifierlockOptions
- contains the lock mode
String getEntityName(Object object)
object
- a persistent entity
IdentifierLoadAccess byId(String entityName)
IdentifierLoadAccess
instance to retrieve the specified entity type by
primary key.
entityName
- The entity name of the entity type to be retrieved
HibernateException
- If the specified entity name cannot be resolved as an entity nameIdentifierLoadAccess byId(Class entityClass)
IdentifierLoadAccess
instance to retrieve the specified entity by
primary key.
entityClass
- The entity type to be retrieved
HibernateException
- If the specified Class cannot be resolved as a mapped entityNaturalIdLoadAccess byNaturalId(String entityName)
NaturalIdLoadAccess
instance to retrieve the specified entity by
its natural id.
entityName
- The entity name of the entity type to be retrieved
HibernateException
- If the specified entity name cannot be resolved as an entity nameNaturalIdLoadAccess byNaturalId(Class entityClass)
NaturalIdLoadAccess
instance to retrieve the specified entity by
its natural id.
entityClass
- The entity type to be retrieved
HibernateException
- If the specified Class cannot be resolved as a mapped entitySimpleNaturalIdLoadAccess bySimpleNaturalId(String entityName)
SimpleNaturalIdLoadAccess
instance to retrieve the specified entity by
its natural id.
entityName
- The entity name of the entity type to be retrieved
HibernateException
- If the specified entityClass cannot be resolved as a mapped entity, or if the
entity does not define a natural-id or if its natural-id is made up of multiple attributes.SimpleNaturalIdLoadAccess bySimpleNaturalId(Class entityClass)
SimpleNaturalIdLoadAccess
instance to retrieve the specified entity by
its simple (single attribute) natural id.
entityClass
- The entity type to be retrieved
HibernateException
- If the specified entityClass cannot be resolved as a mapped entity, or if the
entity does not define a natural-id or if its natural-id is made up of multiple attributes.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 HibernateProxy
true
if the entity or proxy is read-only, false
if the entity or proxy is modifiable.isDefaultReadOnly()
void setReadOnly(Object entityOrProxy, boolean readOnly)
entityOrProxy
- an entity or HibernateProxyreadOnly
- true
if the entity or proxy should be made read-only; false
if the entity or
proxy should be made modifiableTo 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
<T> T doReturningWork(ReturningWork<T> work) throws HibernateException
ReturningWork.execute(java.sql.Connection)
call.
work
- The work to be performed.
ReturningWork.execute(java.sql.Connection)
.
HibernateException
- Generally indicates wrapped SQLException
Connection disconnect()
null
reconnect(Connection)
void reconnect(Connection connection)
connection
- a JDBC connectiondisconnect()
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()
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |