4

Previously I was using the following code to configure the sessionFactory, but after upgrading the version of my hibernate from 4.2.1.Final to 4.3.4.Final, I can not retrieve sessionFactory using the following code as ServiceRegistryBuilder() is deprecated.

I used this link to create it but the provided function is not returning any thing therefore it runs into pre-compile error.

 private static SessionFactory configureSessionFactory() {
        try {
            Configuration configuration = new Configuration();
            configuration.configure();
            serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
            sessionFactory = configuration.buildSessionFactory(serviceRegistry);
            return sessionFactory;
        } catch (HibernateException e) {
            System.out.append("** Exception in SessionFactory **");
            e.printStackTrace();
        }
        return sessionFactory;
    }

     static {
    try {
        sessionFactory = configureSessionFactory();
    } catch (Exception e) {
        System.err.println("%%%% Error Creating SessionFactory %%%%");
        e.printStackTrace();
    }
}

private HibernateUtil() {
}

public static SessionFactory getSessionFactory() {
    return sessionFactory;
}

public static Session getSession() throws HibernateException {
    Session session = threadLocal.get();

    if (session == null || !session.isOpen()) {
        if (sessionFactory == null) {
            rebuildSessionFactory();
        }
        session = (sessionFactory != null) ? sessionFactory.openSession() : null;
        threadLocal.set(session);
    }

    return session;
}

public static void rebuildSessionFactory() {
    try {
        sessionFactory = configureSessionFactory();
    } catch (Exception e) {
        System.err.println("%%%% Error Creating SessionFactory %%%%");
        e.printStackTrace();
    }
}

public static void closeSession() throws HibernateException {
    Session session = (Session) threadLocal.get();
    threadLocal.set(null);

    if (session != null) {
        if (session.isOpen()) {
            session.close();
        }
    }
}
AlexCartio1
  • 238
  • 3
  • 9
  • 29

3 Answers3

7

In 4.3, you should use the StandardServiceRegistryBuilder instead.

import org.hibernate.boot.registry.StandardServiceRegistryBuilder

StandardServiceRegistryBuilder was added as a new parent of ServiceRegistryBuilder. The rest of the code should be the same. The only place that I have found this documented "clearly" is in the 4.3 JavaDocs for ServiceRegistryBuilder.

One thing that I did not notice was that they changed the ServiceRegistryBuilder.buildServiceRegistry() method to just StandardServiceRegistryBuilder.build(). Therefore, it will require you to change that part of your building process:

new ServiceRegistryBuilder()
        .applySettings(configuration.getProperties())
        .buildServiceRegistry();

Becomes:

new StandardServiceRegistryBuilder()
        .applySettings(configuration.getProperties())
        .build();
pickypg
  • 22,034
  • 5
  • 72
  • 84
  • it returns following errors java.lang.reflect.InvocationTargetException, java.lang.NoSuchMethodError: org.hibernate.integrator.internal.IntegratorServiceImpl.(Ljava/util/LinkedHashSet;Lorg/hibernate/boot/registry/classloading/spi/ClassLoaderService;)V – AlexCartio1 Mar 24 '14 at 05:23
  • Messages: org.hibernate.integrator.internal.IntegratorServiceImpl.(Ljava/util/LinkedHashSet;Lorg/hibernate/boot/registry/classloading/spi/ClassLoaderService;)V File: org/hibernate/boot/registry/BootstrapServiceRegistryBuilder.java Line number: 215 – AlexCartio1 Mar 24 '14 at 05:30
  • Make sure that you have removed all of the previous version(s) of Hibernate from your classpath. Looking at it on [github](https://github.com/hibernate/hibernate-orm/blob/4.3.4.Final/hibernate-core/src/main/java/org/hibernate/integrator/internal/IntegratorServiceImpl.java), I do not see why that would be caused _unless_ you were having class loader issues caused by version conflicts, as the `IntegratorServiceImpl` exists in both versions that you are/were using, which suggests a class loader issue to me. – pickypg Mar 24 '14 at 15:24
  • @pickypg please have a look at my question as well, http://stackoverflow.com/questions/23646389/how-to-configure-hibernate-version-4-3-4-final – Jack May 15 '14 at 06:06
0

I tested this one and it works on Hibernate 4.3.6

public class HUtil{   
  private static SessionFactory sessionFactory;
private static ServiceRegistry serviceRegistry;

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

public static SessionFactory getSessionFactory() {
    return createSessionFactory();
}  }
Koala
  • 25
  • 4
-2

Yes, they have deprecated the previous ServiceRegistryBuilder(), here is how you can do it with Hibernate 4.3.4

public void testConnection() throws Exception {

            logger.info("Trying to Connect With a DataBase.");
            Configuration configuration = new Configuration();
            configuration.configure("Your.cfg.xml");
            ServiceRegistryBuilder serviceRegistryBuilder = new ServiceRegistryBuilder().applySettings(configuration
                            .getProperties());
            SessionFactory sessionFactory = configuration
                            .buildSessionFactory(serviceRegistryBuilder.buildServiceRegistry());
            Session session = sessionFactory.openSession();
            logger.info("Database connection created successfuly.");
    }

Source: Create session factory in Hibernate 4

Community
  • 1
  • 1
z atef
  • 7,138
  • 3
  • 55
  • 50
  • 1
    ServiceRegistryBuilder is deprecated on version 4.3.4.Final – AlexCartio1 Mar 24 '14 at 05:24
  • version 4.3.4.Final @AlexCartio1 the details of which version is already mentioned in the title of your question . Anyhow, I added the .3.4 for more clarity. – z atef Mar 24 '14 at 13:50