Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
like image 952
earthmover Avatar asked Feb 03 '14 05:02

earthmover


People also ask

What is ServiceRegistry?

A service registry is a database for the storage of data structures for application-level communication. It serves as a central location where app developers can register and find the schemas used for particular apps.

What is the use of SessionFactory in Hibernate?

Most importantly, the SessionFactory in Hibernate is responsible for the creation of Session objects. The Hibernate Session provides methods such as save, delete and update, all of which are used to perform CRUD-based operations on the database to which the SessionFactory connects.

Can we create multiple SessionFactory in Hibernate?

Q. What we can create more than one sessionFactory in Hibernate. Ans And Its true We can create.As in my app i am able to da same.

Which of these service registries are used by Hibernate?

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.


1 Answers

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.

like image 56
Gajendra Kotra Avatar answered Sep 19 '22 10:09

Gajendra Kotra