Building a SessionFactory

First, we will discuss SessionFactory and how to create it in detail. As the name suggests, a SessionFactory is a factory of sessions.

A SessionFactory has the following features:

  • 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.
  • As a SessionFactory object 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 Session is used to get a physical connectivity with the database.

How to do it…

If you are using a version of hibernate that is earlier than 4, use the following code to create a SessionFactory:

/* Line 1 */ Configuration cfg = new Configuration();
/* Line 2 */ cfg = cfg.configure();
/* Line 3 */ SessionFactory sessionFactory = cfg.buildSessionFactory();

As the buildSessionFactory() method of the Configuration class is deprecated in the version 4 of hibernate, you can use the following code to create a SessionFactory:

/* 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());

How it works…

First of all, let's understand the code from the beginning.

When Line 1 with the Configuration cfg = new Configuration(); code is executed, it creates a blank configuration.

When Line 2 with cfg = cfg.configure(); is executed, the configure() method will look for the hibernate.cfg.xml or hibernate.properties file and then fetch all the properties defined in the configuration and mapping files and filled out in the configuration object.

When 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 Line 2.

For the second part of the code, do the same thing. However, as the API is different, you need to create an instance of StandardServiceRegistryBuilder in Line 3, which works as a builder of the ServiceRegistry interface.

In 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.

There's more…

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 mysql.cfg.xml and 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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.189.251