Friday, July 17, 2015

What is Hibernate SessionFactory?

Hibernate SessionFactory : – SessionFactory is interface which is available in the "org.hibernate" pakcage of the Hibernate distribution.
 There are some key points regarding the SessionFactory : -
1.Single Data Store : – It is single data store for your whole application. Although you can have multiple SessionFactory but each SessionFactory will have one different database associated with it.

2.Thread Safe : – SessionFactory is thread safe so due to this feature many thread can access the SessionFactory.

 3.Immutable : - Once the SessionFactory's object is created you can not change or set the values of Session Facotyr. Its internal state is set at the time of creation.

 4.Singleton : – SessionFactory is built at the time of application startup and it follows the singleton design pattern.

Q.How to create the SessionFactory ?

Ans : Lets assume you have MYSQL database which you are using in your application than for creating the SessionFactory you will write following java code : -
Configuration cfg=new Configuration();

Configuration cfg1=cfg.configure(“hibernate.cfg.xml”);

SessionFactory sf1=cfg1.builed SessionFactory();

Line 1 : – Creates empty configuration object
Line 3 : – When you call cfg.configure("hibernate.cfg.xml") it looks for hibnernate.cfg.xml for Hibernate Mapping
 Line 5 : – SessionFacotry Object will be created and will be used by multiple users for long time

Q.How to create the Multiple SessionFactory ? Ans : So for creating multiple SessionFactory you can different cfg.xml file representing your different database. Lets assume you have two databases MYSQL and ORACLE than i will create two cfg.xml file mysql.cfg.xml oracle.cfg.xml And than for SessionFactory i will write the following code
Configuration cfg=new Configuration();


databaseConfiguration cfg1=cfg.configure(“mysql.cfg.xml”);

SessionFactory sf1=cfg1.builed SessionFactory();

//For Oracle

databaseConfiguration cfg2=cfg.configure(“oracle.cfg.xml”);

SessionFactory sf2=cfg2.builed SessionFactory();

Hope you have liked the above article. Please put your suggestions and questions and i will try to sort it out….


  1. Amazing & Great informative blog,it gives very useful practical information to developer like me. Besides that Wisen has established as Best Hibernate Training in Chennai . or learn thru Online Training mode Hibernate Online Training | Java EE Online Training. Nowadays Hibernate ORM has tons of job opportunities on various vertical industry.

  2. This is an awesome post. Really very informative and creative contents. This concept is a good way to enhance the knowledge. Like it and help me to development very well Thank you for this brief explanation and very nice information. Well got good knowledge.
    Java Training in Gurgaon

  3. Your good knowledge and kindness in playing with all the pieces were very useful. I don’t know what I would have done if I had not encountered such a step like this.
    python Online training in chennai
    python Online training in bangalore
    python interview question and answers

  4. I found your blog while searching for the updates, I am happy to be here. Very useful content and also easily understandable providing.. Believe me I did wrote an post about tutorials for beginners with reference of your blog. 
    rpa training in bangalore
    best rpa training in bangalore
    rpa training in pune | rpa course in bangalore
    rpa training in chennai