org.hibernate.SessionFactory is an interface implemented by the hibernate vendors whose instance is created by calling the buildSessionFactory() which is implemented in the org.hibernate.cfg.Configuration class and must be called with the Configuration class object.

buildSessionFactory() method is org.hibernate.cfg.Configuration class return the SessionFactory interface instance. Internally it returns the SessionFactoryImpl class instance which implements SessionFactory interface, like this

public SessionFactory buildSessionFactory() throws HibernateException{    return new SessionFactoryImpl(this,this.mapping,settings,listeners); }

How to get SessionFactory Instance

1. Create Configuration class object. like this

Configuration cfg=new Configuration();

2. With the Configuration class instance class the Configure() method by specifying the hibernate Config xml means which configuration file is to be read by SessionFactory object and return the Configure class instance.

cfg=cfg.Configure(); 
 or
cfg=cfg.Configure(String resource); //Name of the xml file in 
                                      string format. 

In the above syntax Configure() method without argument by default takes "hibernate.cfg.xml" name as the hibernate Configuration xml file. Or we have any different name then we need to use Configure(String resource) method and mention the file name in the String format.

3. Now after getting the Configuration class instance with the Configure() method , we need to call buildSessionFactory() which finally returns the SessionFactory instance and need to store in the SessionFacroy reference type.

SesionFactory factory=cfg.build.SessionFactory();

Note: buildSessionFactory() method is responsible to parse/read the hibernate configuration xml file and store all the required information in the SessionFactory object.

Now SessionFactory Instance will have information about the driver_class and all other required information about the database. With this we can conclude that one SessionFactory is responsible for single database.
If we want to have multiple database connectivity then we have to declare as much SessionFactory object. 


Why we need to create SessionFactory instance?

It is because SessionFactory Instance is having all the required information about the database for database Connectivity and are responsible for database connectivity and session creation. To know more about Hibernate Session click here  Hibernate Session

Ads 468x60px

.

Ads

.

Featured Posts

Popular Posts

Like Us On FaceBook

Total Pageviews

Online Members

Live Traffic