Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the buildSessionFactory() Configuration method deprecated in Hibernate?

When I updated the Hibernate version from 3.6.8 to 4.0.0, I got a warning about deprecated method buildSessionFactory() in this line:

private static final SessionFactory sessionFactory =
         new Configuration().configure().buildSessionFactory();

the Javadoc recommends using another method

buildSessionFactory(ServiceRegistry serviceRegistry)

but in the documentation I found deprecated variant

like image 307
pushistic Avatar asked Sep 27 '22 00:09

pushistic


People also ask

What is standard service registry in hibernate?

hibernate. service. ServiceRegistry interface. The main purpose of a service registry is to hold, manage and provide access to services. Service registries are hierarchical.

What is SessionFactory in hibernate?

The SessionFactory is a thread safe object and used by all the threads of an application. The SessionFactory is a heavyweight object; it is usually created during application start up and kept for later use. You would need one SessionFactory object per database using a separate configuration file.

How can I open session factory in hibernate?

Hibernate SessionFactory openSession() method always opens a new session. We should close this session object once we are done with all the database operations. We should open a new session for each request in multi-threaded environment.


1 Answers

Yes it is deprecated. Replace your SessionFactory with the following:

In Hibernate 4.0, 4.1, 4.2

private static SessionFactory sessionFactory;
private static ServiceRegistry serviceRegistry;

public static SessionFactory createSessionFactory() {
    Configuration configuration = new Configuration();
    configuration.configure();
    ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(
            configuration.getProperties()). buildServiceRegistry();
    sessionFactory = configuration.buildSessionFactory(serviceRegistry);
    return sessionFactory;
}

UPDATE:

In Hibernate 4.3 ServiceRegistryBuilder is deprecated. Use the following instead.

serviceRegistry = new StandardServiceRegistryBuilder().applySettings(
            configuration.getProperties()).build();
like image 383
batbaatar Avatar answered Sep 28 '22 13:09

batbaatar