In Hibernate, what is SessionFactory?

Answered

In Hibernate, what is SessionFactory? Is it thread-safe?

Ninja Asked on 17th September 2018 in Hibernate.
Add Comment
1 Answer(s)
Best answer

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.

Ninja Answered on 17th September 2018.
Add Comment