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:
If the Session throws an exception, the transaction must be rolled back
and the session discarded. The internal state of the Session might not
be consistent with the database after the exception occurs.
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(); }If the Session throws an exception, the transaction must be rolled back and the session discarded. The internal state of the Session might not be consistent with the database after the exception occurs.