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….
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.
ReplyDeleteJava Training in Gurgaon
Thanks for splitting your comprehension with us. It’s really useful to me & I hope it helps the people who in need of this vital information.
ReplyDeleteData Science course in Indira nagar
Data Science course in marathahalli
Data Science Interview questions and answers
Data science training in tambaram
Data Science course in btm layout
Data science course in kalyan nagar
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.
ReplyDeleterpa training in bangalore
best rpa training in bangalore
rpa training in pune | rpa course in bangalore
rpa training in chennai
Nice Blog. really thanks for sharing this blog. the blog is very informative.
ReplyDeleteData Science Training Course In Chennai | Data Science Training Course In Anna Nagar | Data Science Training Course In OMR | Data Science Training Course In Porur | Data Science Training Course In Tambaram | Data Science Training Course In Velachery
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
ReplyDeletePython Training in Gurgaon