What is the use of ServiceRegistry in creating SessionFactory
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);
Please refer Hibernate documentation (chapter 7) : http://docs.jboss.org/hibernate/core/4.0/devguide/en-US/html/ch07.html
Service : Services are classes that provide Hibernate with pluggable implementations of various types of functionality. Specifically they are implementations of certain service contract interfaces
ServiceRegistry : The central service API, aside from the services themselves, is the org.hibernate.service.ServiceRegistry interface. The main purpose of a service registry is to hold, manage and provide access to services.
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.
- 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)
org.hibernate.stat.spi.StatisticsImplementor is the SPI portion of the org.hibernate.stat.Statistics API. The collector portion, if you will.