Hibernate Tutorials/
« Previous | 1 | 2 | 3 | Next »
A Session is used to get a physical connection with a database. The Session object is lightweight and designed to be instantiated each time an interaction is needed with the database. Persistent objects are saved and retrieved through a Session object.

The session objects should not be kept open for a long time because they are not usually thread safe and they should be created and destroyed them as needed. 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 the following three states at a given point in time:
  • transient: A new instance of a persistent class which is not associated with a Session and has no representation in the database and no identifier value is considered transient by Hibernate.
  • persistent: You can make a transient instance persistent by associating it with a Session. A persistent instance has a representation in the database, an identifier value and is associated with a Session.
  • detached: Once we close the Hibernate Session, the persistent instance will become a detached instance.
A Session instance is serializable if its persistent classes are serializable. A typical transaction should use the following idiom:

Session session =factory.openSession();
tx= session.beginTransaction();
// do some work
catch(Exception e){
if(tx!=null) tx.rollback();

If the Session throws an exception, the transaction must be rolled back and the session must be discarded.
« Previous | 1 | 2 | 3 | Next »

copyright © 2014 - all rights riserved by javatechnologycenter.com