Hibernate SessionFactory vs Service Registry
Asked Answered
T

1

7

Prior to Hibernate 4

The way of implementation of session connection is only with SessionFactory.

Example:

Configuration cfg=new Configuration();
Configuration cfg=cfg.configure(“mysql.cfg.xml”);
SessionFactory sf=cfg1.buildSessionFactory();

Hibernate 4.0,4.1,4.2

It changes with the concept of ServiceRegistry and the way of implementation is with ServiceRegistry & SessionFactory.

Example:

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

 serviceRegistry = new ServiceRegistryBuilder().applySettings(
            configuration.getProperties()).build();
 sessionFactory = configuration.buildSessionFactory(serviceRegistry);
 return sessionFactory;

Hibernate 4.3

ServiceRegistryBuilder() also deprecated and replaced with StandardServiceRegistryBuilder().

Example:

serviceRegistry = new StandardServiceRegistryBuilder().applySettings(
                configuration.getProperties()).build();

My questions are:

  1. What is the difference b/w SessionFactory & ServiceRegistry ?
  2. Why Concept of ServiceRegistry is required ?
  3. What is the advantage of introduce ServiceRegistry over SessionFactory ?
  4. What is the difference b/w ServiceRegistryBuilder() & StandardServiceRegistryBuilder() ?
Theresa answered 28/5, 2014 at 6:56 Comment(2)
hibernate.atlassian.net/browse/HHH-2578 check thisMileage
I'm amazed why a question this important hasn't been answered yet, not even marked as answered.Bacciferous
D
0

You can access to the SessionFactory object in two ways

first:

ServiceRegistry sr = new StandardServiceRegistryBuilder().applySettings(con.getProperties()).build();

SessionFactory factory = con.buildSessionFactory(sr);

second:

Configuration cfg = new Configuration().configure();
SessionFactory factory = cfg.buildSessionFactory();

both of them use StandardServiceRegistry, but second one is shorter and use StandardServiceRegistry in the configure() method and you don't see it in your code

Diapophysis answered 21/1, 2021 at 14:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.