In Hibernate, what is SessionFactory?
In Hibernate, what is SessionFactory? Is it thread-safe?
SessionFactory in Hibernate is a factory object used to create a session instance. It is part of org.hibernate package. It is a multithreaded object and hence it is thread-safe. Many threads can access it concurrently.
SessionFactory can be created through the hibernate configuration file (hibernate.cfg.xml) which determines the hibernate mapping file (.hbm) to be loaded. These files contain the properties required for configuring the database and the tables.
SessionFactory can be created as follows:
Configuration cfg=new Configuration(); cfg=cfg.configure();
// When you invoke the configure() method, the framework looks for hibernate.cfg.xml and for hibernate mapping file (.hbm file).
SessionFactory sc=cfg.buildSessionFactory();
// When you invoke the buildSessionFactory() method on the configuration object, the SessionFactory object is created.