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