What is the use of ServiceRegistry in creating SessionFactory

hibernate, java, sessionfactory

Solution

A ServiceRegistry, at its most basic, hosts and manages Services. Its contract is defined by the org.hibernate.service.ServiceRegistry interface. Currently Hibernate utilizes 3 different ServiceRegistry implementations forming a hierarchy.

- BootstrapServiceRegistry

- StandardServiceRegistry

- SessionFactoryServiceRegistry org.hibernate.service.spi.SessionFactoryServiceRegistry is the 3rd standard Hibernate ServiceRegistry. Typically, its parent registry is the StandardServiceRegistry. SessionFactoryServiceRegistry is designed to hold Services which need access to the SessionFactory. Currently that is just 3 Services.

EventListenerRegistry org.hibernate.event.service.spi.EventListenerRegistry is the big Service managed in the SessionFactoryServiceRegistry. This is the Service that manages and exposes all of Hibernate’s event listeners. A major use-case for Integrators is to alter the listener registry.

If doing custom listener registration, it is important to understand the org.hibernate.event.service.spi.DuplicationStrategy and its effect on registration. The basic idea is to tell Hibernate:

what makes a listener a duplicate

how to handle duplicate registrations (error, first wins, last wins)

StatisticsImplementor

org.hibernate.stat.spi.StatisticsImplementor is the SPI portion of the org.hibernate.stat.Statistics API. The collector portion, if you will.

Problem

I am learning Hibernate in Java. Since, to create a `Session`, we have to use `SessionFactory.openSession()`, and for creating `SessionFactory` we use `sessionFactory = config.buildSessionFactory(serviceRegistry);` What is the use of `ServiceRegistry` in hibernate?? My code for creating `SessionFactory` : ``` Configuration config = new Configuration(); config.addAnnotatedClass(user.class); config.addAnnotatedClass(emp.class); config.configure(); // Didn't understand the code below Properties configProperties = config.getProperties(); ServiceRegistryBuilder serviceRegisteryBuilder = new ServiceRegistryBuilder(); ServiceRegistry serviceRegistry = serviceRegisteryBuilder.applySettings(configProperties).buildServiceRegistry(); SessionFactory sessionFactory = config.buildSessionFactory(serviceRegistry); ```

Original source