|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
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, find and delete operations
for instances of mapped entity classes. Instances may exist in one of two states:
transient: not associated with any Session
persistent: associated with a Session
Transient instances may be made persistent by calling save(),
update() or saveOrUpdate(). Persistent instances may be made transient
by calling delete(). Any instance returned by a find(), iterate() or
load() method is persistent.
save() results in an SQL INSERT, delete()
in an SQL DELETE and update() in an SQL UPDATE. Changes to
persistent instances are detected at flush time and also result in an SQL
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 execution of the current query. |
void |
clear()
Completely clear the session. |
Connection |
close()
End the Session by disconnecting from the JDBC connection and cleaning up. |
Connection |
connection()
Get the JDBC connection. |
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. |
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 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)
Remove a persistent instance from the datastore. |
int |
delete(String query)
Delete all objects returned by the query. |
int |
delete(String query,
Object[] values,
Type[] types)
Delete all objects returned by the query. |
int |
delete(String query,
Object value,
Type type)
Delete all objects returned by the query. |
Connection |
disconnect()
Disconnect the Session from the current JDBC connection. |
void |
evict(Object object)
Remove this instance from the session cache. |
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)
Execute a query. |
List |
find(String query,
Object[] values,
Type[] types)
Execute a query with bind parameters. |
List |
find(String query,
Object value,
Type type)
Execute a query with bind parameters. |
void |
flush()
Force the 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. |
LockMode |
getCurrentLockMode(Object object)
Determine the current lock mode of the given object. |
FlushMode |
getFlushMode()
Get the current flush mode. |
Serializable |
getIdentifier(Object object)
Return the identifier of an entity instance cached by the Session, or throw an exception if the instance is transient or associated with a different Session. |
Query |
getNamedQuery(String queryName)
Obtain an instance of Query for a named query string defined in the mapping file. |
SessionFactory |
getSessionFactory()
Get the SessionFactory that created this instance. |
boolean |
isConnected()
Check if the Session is currently connected. |
boolean |
isDirty()
Does this Session contain any changes which must be synchronized with the database? Would any SQL be executed if we flushed this session? |
boolean |
isOpen()
Check if the Session is still open. |
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. |
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. |
void |
lock(Object object,
LockMode lockMode)
Obtain the specified lock level upon the given object. |
void |
reconnect()
Obtain a new JDBC connection. |
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 all reachable transient objects, reusing the current identifier values. |
Serializable |
save(Object object)
Persist the given transient instance, first assigning a generated identifier. |
void |
save(Object object,
Serializable id)
Persist the given transient instance, using the given identifier. |
void |
saveOrUpdate(Object object)
Either save() or update() the given instance, depending upon the value of its identifier property. |
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. |
void |
setFlushMode(FlushMode flushMode)
Set the flush mode. |
void |
update(Object object)
Update the persistent instance with the identifier of the given transient instance. |
void |
update(Object object,
Serializable id)
Update the persistent state associated with the given identifier. |
Method Detail |
public void flush() throws HibernateException
HibernateException
public void setFlushMode(FlushMode flushMode)
flushMode
- the FlushModeFlushMode
public FlushMode getFlushMode()
public SessionFactory getSessionFactory()
SessionFactory
public Connection connection() throws HibernateException
HibernateException
- if the Session is disconnectedpublic Connection disconnect() throws HibernateException
HibernateException
- if the Session is disconnectedreconnect()
public void reconnect() throws HibernateException
HibernateException
disconnect()
public void reconnect(Connection connection) throws HibernateException
connection
- a JDBC connection
HibernateException
- if the Session is connecteddisconnect()
public Connection close() throws HibernateException
HibernateException
public void cancelQuery() throws HibernateException
HibernateException
public boolean isOpen()
public boolean isConnected()
public boolean isDirty() throws HibernateException
HibernateException
public Serializable getIdentifier(Object object) throws HibernateException
object
- a persistent instance
HibernateException
- if the Session is connectedpublic boolean contains(Object object)
object
- an instance of a persistent class
public void evict(Object object) throws HibernateException
object
- a persistent instance
HibernateException
public 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
public Object load(Class theClass, Serializable id) throws HibernateException
theClass
- a persistent classid
- a valid identifier of an existing persistent instance of the class
HibernateException
public 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
public void replicate(Object object, ReplicationMode replicationMode) throws HibernateException
object
- a transient instance of a persistent class
HibernateException
public Serializable save(Object object) throws HibernateException
object
- a transient instance of a persistent class
HibernateException
public void save(Object object, Serializable id) throws HibernateException
object
- a transient instance of a persistent classid
- an unused valid identifier
HibernateException
public void saveOrUpdate(Object object) throws HibernateException
object
- a transient instance containing new or updated state
HibernateException
save(java.lang.Object)
,
update(Object object, Serializable id)
public void update(Object object) throws HibernateException
object
- a transient instance containing updated state
HibernateException
public void update(Object object, Serializable id) throws HibernateException
object
- a transient instance containing updated stateid
- identifier of persistent instance
HibernateException
public Object saveOrUpdateCopy(Object object) throws HibernateException
object
- a transient instance with state to be copied
HibernateException
public Object saveOrUpdateCopy(Object object, Serializable id) throws HibernateException
object
- a persistent or transient instance with state to be copiedid
- the identifier of the instance to copy to
HibernateException
public void delete(Object object) throws HibernateException
object
- the instance to be removed
HibernateException
public List find(String query) throws HibernateException
query
- a query expressed in Hibernate's query language
HibernateException
public List find(String query, Object value, Type type) throws HibernateException
query
- the query stringvalue
- a value to be bound to a "?" placeholder (JDBC IN parameter).type
- the Hibernate type of the value
HibernateException
for access to Type instances
public List find(String query, Object[] values, Type[] types) throws HibernateException
query
- the query stringvalues
- an array of values to be bound to the "?" placeholders (JDBC IN parameters).types
- an array of Hibernate types of the values
HibernateException
for access to Type instances
public Iterator iterate(String query) throws HibernateException
query
- the query string
HibernateException
public Iterator iterate(String query, Object value, Type type) throws HibernateException
query
- the query stringvalue
- a value to be witten to a "?" placeholder in the query stringtype
- the hibernate type of value
HibernateException
public Iterator iterate(String query, Object[] values, Type[] types) throws HibernateException
query
- the query stringvalues
- a list of values to be written to "?" placeholders in the querytypes
- a list of Hibernate types of the values
HibernateException
public Collection filter(Object collection, String filter) throws HibernateException
collection
- a persistent collection to filterfilter
- a filter query string
HibernateException
public Collection filter(Object collection, String filter, Object value, Type type) throws HibernateException
collection
- a persistent collection to filterfilter
- a filter query stringvalue
- a value to be witten to a "?" placeholder in the query stringtype
- the hibernate type of value
HibernateException
public Collection filter(Object collection, String filter, Object[] values, Type[] types) throws HibernateException
collection
- a persistent collection to filterfilter
- a filter query stringvalues
- a list of values to be written to "?" placeholders in the querytypes
- a list of Hibernate types of the values
HibernateException
public int delete(String query) throws HibernateException
query
- the query string
HibernateException
public int delete(String query, Object value, Type type) throws HibernateException
query
- the query stringvalue
- a value to be witten to a "?" placeholder in the query string.type
- the hibernate type of value.
HibernateException
public int delete(String query, Object[] values, Type[] types) throws HibernateException
query
- the query stringvalues
- a list of values to be written to "?" placeholders in the query.types
- a list of Hibernate types of the values
HibernateException
public void lock(Object object, LockMode lockMode) throws HibernateException
object
- a persistent or transient instancelockMode
- the lock level
HibernateException
public void refresh(Object object) throws HibernateException
object
- a persistent or transient instance
HibernateException
public void refresh(Object object, LockMode lockMode) throws HibernateException
object
- a persistent or transient instancelockMode
- the lock mode to use
HibernateException
public LockMode getCurrentLockMode(Object object) throws HibernateException
object
- a persistent instance
HibernateException
public Transaction beginTransaction() throws HibernateException
HibernateException
Transaction
public Criteria createCriteria(Class persistentClass)
persistentClass
-
public Query createQuery(String queryString) throws HibernateException
queryString
- a Hibernate query
HibernateException
public Query createFilter(Object collection, String queryString) throws HibernateException
collection
- a persistent collectionqueryString
- a Hibernate query
HibernateException
public Query getNamedQuery(String queryName) throws HibernateException
queryName
- the name of a query defined externally
HibernateException
public Query createSQLQuery(String sql, String returnAlias, Class returnClass)
sql
- a query expressed in SQLreturnAlias
- a table alias that appears inside {} in the SQL stringreturnClass
- the returned persistent classpublic Query createSQLQuery(String sql, String[] returnAliases, Class[] returnClasses)
sql
- a query expressed in SQLreturnAliases
- an array of table aliases that appear inside {} in the SQL stringreturnClasses
- the returned persistent classespublic void clear()
public Object get(Class clazz, Serializable id) throws HibernateException
clazz
- a persistent classid
- an identifier
HibernateException
public Object get(Class clazz, Serializable id, LockMode lockMode) throws HibernateException
clazz
- a persistent classid
- an identifierlockMode
- the lock mode
HibernateException
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |