Monday, January 25, 2016

How you can build a SessionFactory object and pull out a Session


In Hibernate old versions (Prior to 4.0), we can build a SessionFactory object and pull out a Session as follows:
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();

Session session = sessionFactory.openSession();

However, since Hibernate 4.0, this approach is deprecated. and we are using ServiceRegistryBuilder class to create SessionFactory as follows:
Configuration configuration = new Configuration().configure();

ServiceRegistryBuilder registry = new ServiceRegistryBuilder();

registry.applySettings(configuration.getProperties());

ServiceRegistry serviceRegistry = registry.buildServiceRegistry();

SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
 
Session session = sessionFactory.openSession();


However, since Hibernate 4.3, this approach is also deprecated. and we are using StandardServiceRegistryBuilder class to create SessionFactory as follows:

Configuration configuration = new Configuration().configure();

StandardServiceRegistryBuilder registry = new StandardServiceRegistryBuilder();

registry.applySettings(configuration.getProperties());

ServiceRegistry serviceRegistry = registry.build();

SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);

Session session = sessionFactory.openSession();

How StandardServiceRegistryBuilder class is differ from ServiceRegistryBuilder?
  • in StandardServiceRegistryBuilder, we are using build() function to generate serviceRegistery object whereas in ServiceRegistryBuilder, we was using buildServiceRegistry() function.
  • StandardServiceRegistryBuilder is in "org.hibernate.service.ServiceRegistryBuilder" package whereas ServiceRegistryBuilder class is in "org.hibernate.service.ServiceRegistryBuilder" package
How you can make your own utility class to generate SessionFactory class object?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package com.sachin.hibernate;
 
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
 
public class HibernateUtil {
    private static SessionFactory sessionFactory;
     
    public static SessionFactory getSessionFactory() {
        if (sessionFactory == null) {
            // loads configuration and mappings
            Configuration configuration = new Configuration().configure();
            ServiceRegistry serviceRegistry
                new StandardServiceRegistryBuilder()
                    .applySettings(configuration.getProperties()).build();
             
            // builds a session factory from the service registry
            sessionFactory = configuration.buildSessionFactory(serviceRegistry);           
        }
         
        return sessionFactory;
    }
}
How you can use above utility class in your Hibernate application?
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
 
Session session = sessionFactory.openSession();
session.beginTransaction();
session.save(object);

if you like this post then comment, share and give suggestion so that i can give my best in java framework.