what is Session factory in Hibernate?

Here we can explain what is SessionFactory..
  • SessionFactory is an interface, which is available in “org.hibernate” package.
  • Session factory is long live multithreaded object.
  • Usually one session factory should be created for one database.
  • When you have multiple databases in your application you should create multiple SessionFactory object.
  • Assume the scenario that you are using one database called mysql in your application then following is the way to create the SessionFactory object.

Configuration cfg=new Configuration();  
//Empty object will be created.
cfg=cfg.configure();

Here when you called  configure()  method  It looks for hibernate cfg.xml and for Hibernate mapping file.

Filled with all the properties defined in the configuration documents and mapping documents

SessionFactory sc=cfg.buildSessionFactory();
  • SessionFactory object will be created once and will be used by multiple users for long time.
  • Session Factory object is the factory for session objects.
If you are using  two databases called mysql and oracle in your hibernate application then you need to build 2 SessionFactory object


Configuration cfg=new Configuration();
Configuration cfg1=cfg.configure(“mysql.cfg.xml”);
SessionFactory sf1=cfg1.builed SessionFactory();

Configuration cfg2=cfg.configure(“oracle.cfg.xml”);
SessionFactory sf2=cfg2.builed SessionFactory();

When we are using more than one database in our application than we use the HibernateUtil class which is implemented based on singleton design pattern which insure that one and only one sessionFactory object will be created for entire application

33 comments :

  1. got some idea about session factory :)

    ReplyDelete
  2. way of explanation is good

    ReplyDelete
  3. Good Explanation about session factory

    ReplyDelete
  4. Super Se Uperrrrrrrrr

    ReplyDelete
  5. What is the Exact use of hibernatUtil class please expain me waiting ???

    ReplyDelete
  6. Nice Explanation

    ReplyDelete
  7. got some idea about it :)

    ReplyDelete
  8. very good explanation i want more information like this

    ReplyDelete
  9. really good explanation

    ReplyDelete
  10. Superb ..today i failed to answer this question in an interview :( ...
    Cleared my doubt .. Thanks a lot

    ReplyDelete
  11. Thanks a lot it helped..:)

    ReplyDelete
  12. Good Explanation...

    ReplyDelete
  13. Good article!!!

    ReplyDelete
  14. simple and superr

    ReplyDelete
  15. Few more additional details regarding the session factory of Hibernate can be found here

    http://techpost360.blogspot.in/2015/07/what-is-hibernate-sessionfactory.html

    ReplyDelete
  16. a verygood explanation . thanks buddy

    ReplyDelete
  17. good explanation

    ReplyDelete
  18. good explanation

    ReplyDelete
  19. bada badhiya explanatition rahe..ehe tarah ke informationwa auro chahela

    ReplyDelete
  20. thnks ...nice...

    ReplyDelete