Pages

Important objects in Hibernate persistent logic

There are 3 important objects in Hibernate persistent logic

1. Configuration Object
2. SessionFactory Object
3. Session Object



Configuration Object 

Org.hibernate.cfg.Configuration class 

This object is used for activating/bootstrapping the Hibernate (HB) framework.

This object reads HB configuration file and HB mapping file and put the content in-memeory

It is usually created only once during application initialization.

eg:

// Activate HB framework
Configuration cfg = new Configuration();
 
// load and check for validation and well-formed
cfg = cfg.configure(“/com/mt/hibernate.cfg.xml”); 

SessionFactory Object

Factory to create session object, based on Factory design pattern

Object of a class that implements org.hibernate.SessionFactroty interface

It is immutable object

It is heavy weight objects

It is thread-safe

Created  using configuration object

eg:

SessionFactory factory = cfg.buildSessionFactory();

using cfg.buildSessionFactory() we can read the in-memory configuration and we get JDBC property like user name, password, url,

and using this information it create the set of connection objects to DB,

and using these connection objects it creates connection pool

Session Object :

Not related to Servlet session

created based on SessionFactory Object

It take one connection object from connection pool represented by
SessionFactory object.

Open connection to DB and allows the application to perform object based persistence operation on DB

It is light weight object

It is not thread safe object

Object of a class that implements org.hibernate.Session interface

eg :

Session sess = factory.openSession();

sess.save(obj); // save record to DB represented by this object

sess.delete(obj);

sess.update(obj);

sess.load(Domain class,id);

No comments:

Post a Comment