Tuesday, June 9, 2009

What is Hibernate Session

Session is hibernate construct which is used to mediate connection with database.

At the time session is created, it is creating connection with database (Hibernate is use configurations given by configuration file to make the connection). Then session is holding connection until session ends. All objects loaded by Hibernate are associated with session. When session is ends Hibernate will automatically close the connection.

To use Hibernate persistence mechanism you have to initialize the Hibernate environment and then need to get session object from SessionFactory class. I'm showing below how to obtain session from SessionFactory .

// initializing Hibernate environment
Configuration cfg = new Configuration().configure();

// create session factory
SessionFactory factory = cfg.buildSessionFactory();

// getting the new session object
Session session = factory.openSession();

No comments:

Post a Comment