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

Related Posts:

  • Open Session In View(OSIV) Once the session is closed you can not be able to access the data from detached object and if you do you will get a LazyInitializationException: Session has been closed. It means that your session and session transaction is… Read More
  • Hibernate Architecture Configuration or AnnotationConfiguration : These are available in org.hibernate.cfg package. This is the first class that will be instantiated by hibernate application. You can use Configuration or AnnotationConfigurati… Read More
  • What is n+1 problem and how to solve it N+1 is one of the most famous question among java developers. It will give some performance issue if you where working on a large project. So it's better to resolve it using some below techniques. What is n+1 problem ? Ans… Read More
  • Alternatives of Open Session In View(OSIV) Today I am going to tell you other alternative you can do in order to avoid LazyInitializationException: Session has been closed. If you are using SPRING with HIBERNATE that you can take the advantages of IOC and AOP. These … Read More
  • Hibernate Fetching Strategies Today i am going to tell you what are the Hibernate Fetching Strategies and how to use that in order to tune the performance of your applications. So these are the fetching strategies in hibernate : join fetching(fetch="jo… Read More

0 comments:

Post a Comment

Ads 468x60px

.

Ads

.

Featured Posts

Popular Posts

Like Us On FaceBook

Total Pageviews

201978

Online Members

Live Traffic