It's an interface implemented using the singleton pattern.
It's created using the configuration provided by the configuration file.
It's thread-safe, so it's created once during the application's lifetime, and multiple users or threads can access it at the same time without any concurrency issue.
SessionFactoryobject is immutable, changes made to the configuration will not affect the existing factory object.
It's a factory class, and its main duty is to create, manage, and retrieve a session on request. A
Sessionis used to get a physical connectivity with the database.
If you are using a version of hibernate that is earlier than 4, use the following code to create a
/* Line 1 */ Configuration cfg = new Configuration(); /* Line 2 */ cfg = cfg.configure(); /* Line 3 */ SessionFactory sessionFactory = cfg.buildSessionFactory();
buildSessionFactory() method of the
Configuration class is deprecated in the version 4 of hibernate, you can use the following code to create a
/* Line 1 */ Configuration configuration = new Configuration(); /* Line 2 */ configuration = configuration.configure(); /* Line 3 */ StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder(); /* Line 4 */ builder = builder.applySettings(configuration.getProperties()); /* Line 5 */SessionFactory sessionFactory = configuration.buildSessionFactory(builder.build());
First of all, let's understand the code from the beginning.
Line 1 with the
Configuration cfg = new Configuration(); code is executed, it creates a blank
Line 2 with
cfg = cfg.configure(); is executed, the
configure() method will look for the
hibernate.properties file and then fetch all the properties defined in the configuration and mapping files and filled out in the
Line 3 with
SessionFactory sessionFactory = cfg.buildSessionFactory(); is executed, the preceding code builds
SessionFactory using the
Configuration object. It actually creates the
SessionFactory object using the configuration loaded in
For the second part of the code, do the same thing. However, as the API is different, you need to create an instance of
Line 3, which works as a builder of the
Line 4, apply all the settings that are loaded into the configuration object. In the last line,
Line 5, create an object of
SessionFactory, the configuration being set by the builder itself.
If we want to connect two different databases in an application, we need to create two different
SessionFactory objects in it. Let's see how to do this.
For example, if we have two different databases, MySQL and PostgreSQL, we will create two different CFG files called
postgresql.cfg.xml. Then, we will just create a
SessionFactory, as shown in the following code:
Configuration configurationMySQL = new Configuration().configure("mysql.cfg.xml"); SessionFactory sessionFactoryMySQL = configurationMySQL .buildSessionFactory(); Configuration configurationPostgresql = new Configuration().configure("postgresql.cfg.xml"); SessionFactory sessionFactoryPostgresql = configurationPostgresql .buildSessionFactory();
Now, we have two different
SessionFactory objects that we can use as per our requirement.