|
||||||||||
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
Method Summary | |
---|---|
Transaction |
beginTransaction()
Begin a unit of work and return the associated Transaction object. |
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. To be replaced with a SPI for performing work against the connection; scheduled for removal in 4.x |
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 |
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 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)
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. |
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. |
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. |
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 |
isOpen()
Check if the session is still open. |
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)
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. |
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)
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 |
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. |
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. |
Method Detail |
---|
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
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 checkingSerializable getIdentifier(Object object) throws HibernateException
object
- a persistent instance
TransientObjectException
- if the instance is transient or associated with
a different session
HibernateException
boolean contains(Object object)
object
- an instance of a persistent class
void evict(Object object) throws HibernateException
object
- a persistent instance
HibernateException
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 level
HibernateException
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 level
HibernateException
Object load(Class theClass, Serializable id) throws HibernateException
theClass
- a persistent classid
- a valid identifier of an existing persistent instance of the class
HibernateException
Object load(String entityName, Serializable id) throws HibernateException
entityName
- a persistent classid
- a valid identifier of an existing persistent instance of the class
HibernateException
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 class
HibernateException
void replicate(Object object, ReplicationMode replicationMode) throws HibernateException
object
- a detached instance of a persistent class
HibernateException
void replicate(String entityName, Object object, ReplicationMode replicationMode) throws HibernateException
object
- a detached instance of a persistent class
HibernateException
Serializable save(Object object) throws HibernateException
object
- a transient instance of a persistent class
HibernateException
Serializable save(String entityName, Object object) throws HibernateException
object
- a transient instance of a persistent class
HibernateException
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 state
HibernateException
save(Object)
,
update(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".
entityName
- The name of the entityobject
- a transient or detached instance containing new or updated state
HibernateException
save(String,Object)
,
update(String,Object)
void update(Object object) throws HibernateException
object
- a detached instance containing updated state
HibernateException
void update(String entityName, Object object) throws HibernateException
object
- a detached instance containing updated state
HibernateException
Object merge(Object object) throws HibernateException
object
- a detached instance with state to be copied
HibernateException
Object merge(String entityName, Object object) throws HibernateException
object
- a detached instance with state to be copied
HibernateException
void persist(Object object) throws HibernateException
object
- a transient instance to be made persistent
HibernateException
void persist(String entityName, Object object) throws HibernateException
object
- a transient instance to be made persistent
HibernateException
void delete(Object object) throws HibernateException
object
- the instance to be removed
HibernateException
void delete(String entityName, Object object) throws HibernateException
entityName
- The entity name for the instance to be removed.object
- the instance to be removed
HibernateException
void lock(Object object, LockMode lockMode) throws HibernateException
object
- a persistent or transient instancelockMode
- the lock level
HibernateException
void lock(String entityName, Object object, LockMode lockMode) throws HibernateException
object
- a persistent or transient instancelockMode
- the lock level
HibernateException
void refresh(Object object) throws HibernateException
object
- a persistent or detached instance
HibernateException
void refresh(Object object, LockMode lockMode) throws HibernateException
object
- a persistent or detached instancelockMode
- the lock mode to use
HibernateException
LockMode getCurrentLockMode(Object object) throws HibernateException
object
- a persistent instance
HibernateException
Transaction beginTransaction() throws HibernateException
HibernateException
Transaction
Transaction getTransaction()
HibernateException
Transaction
Criteria createCriteria(Class persistentClass)
persistentClass
- a class, which is persistent, or has persistent subclasses
Criteria createCriteria(Class persistentClass, String alias)
persistentClass
- a class, which is persistent, or has persistent subclasses
Criteria createCriteria(String entityName)
entityName
-
Criteria createCriteria(String entityName, String alias)
entityName
-
Query createQuery(String queryString) throws HibernateException
queryString
- a HQL query
HibernateException
SQLQuery createSQLQuery(String queryString) throws HibernateException
queryString
- a SQL query
HibernateException
Query createFilter(Object collection, String queryString) throws HibernateException
collection
- a persistent collectionqueryString
- a Hibernate query
HibernateException
Query getNamedQuery(String queryName) throws HibernateException
queryName
- the name of a query defined externally
HibernateException
void clear()
Object get(Class clazz, Serializable id) throws HibernateException
clazz
- a persistent classid
- an identifier
HibernateException
Object get(Class clazz, Serializable id, LockMode lockMode) throws HibernateException
clazz
- a persistent classid
- an identifierlockMode
- the lock mode
HibernateException
Object get(String entityName, Serializable id) throws HibernateException
entityName
- the entity nameid
- an identifier
HibernateException
Object get(String entityName, Serializable id, LockMode lockMode) throws HibernateException
entityName
- the entity nameid
- an identifierlockMode
- the lock mode
HibernateException
String getEntityName(Object object) throws HibernateException
object
- a persistent entity
HibernateException
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()
void setReadOnly(Object entity, boolean readOnly)
Query.setReadOnly(boolean)
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 connection
HibernateException
disconnect()
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |