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();

//For MYSQL

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….

5 comments:

  1. 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

    ReplyDelete
  2. 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

    ReplyDelete
  3. Truly a very good article on how to handle the future technology. After reading your post, thanks for taking the time to discuss this, I feel happy about and I love learning more about this topic

    Python Training in Gurgaon

    ReplyDelete