Learn How to create SessionFactory in Hibernate 4.3.1?
You you know Hibernate 4.3.x is already released and it is fully supporting the JPA 2.1 specification. Since the Hibernate API is changed and many things are deprecated, you should use the new way of initializing the SessionFactory. Here in this tutorial we are describing the code to create SessionFactory in Hibernate 4.3.x.
Now we should use the class StandardServiceRegistryBuilder for building the SessionFactory in the Hibernate 4.3.0, 4.3.1 and above releases. The StandardServiceRegistryBuilder class uses the default hibernate.cfg file as a configuration file.
We have used the build() method of the StandardServiceRegistryBuilder class which returns the object of StandardServiceRegistry. The StandardServiceRegistry is the interface.
Finally we are calling the buildSessionFactory() on the instance of Configuration for getting the SessionFactory object.
How to create SessionFactory in Hibernate 4.3.1?
In Hibernate 4.3.0 and above versions there are many change in the API. Here are the few errors you will be encountered if you don't create the SessionFactory in correct way:
* The type ServiceRegistryBuilder is deprecated
* The method buildServiceRegistry() is undefined for the type
ServiceRegistryBuilder
* The method buildSessionFactory() from the type Configuration is deprecated
So, overcome these errors the correct way to create the SessionFactory is given below:
Configuration cfg = new Configuration().configure("hibernate.cfg.xml"); StandardServiceRegistryBuilder sb = new StandardServiceRegistryBuilder(); sb.applySettings(cfg.getProperties()); StandardServiceRegistry standardServiceRegistry = sb.build(); sessionFactory = cfg.buildSessionFactory(standardServiceRegistry);
Following is the code of the HibernateUtil.java file which shows you how to get the instance of SessionFactory correctly in Hibernate 4.3.0, Hibernate 4.3.1 final and future versions of Hibernate 4.3.x:
package net.roseindia; import org.hibernate.SessionFactory; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.Configuration; import org.hibernate.boot.registry.StandardServiceRegistry; /** * @author Deepak Kumar * * Web: http://www.roseindia.net */ public class HibernateUtil { private static final SessionFactory sessionFactory; static { try { Configuration cfg = new Configuration().configure("hibernate.cfg.xml"); StandardServiceRegistryBuilder sb = new StandardServiceRegistryBuilder(); sb.applySettings(cfg.getProperties()); StandardServiceRegistry standardServiceRegistry = sb.build(); sessionFactory = cfg.buildSessionFactory(standardServiceRegistry); } catch (Throwable th) { System.err.println("Enitial SessionFactory creation failed" + th); throw new ExceptionInInitializerError(th); } } public static SessionFactory getSessionFactory() { return sessionFactory; } }
Check the tutorial Hibernate 4 Hello World: Example of Hello World program for complete code of creating the SessionFactory instance.
Read Complete Hibernate 4.3.x tutorial at Hibernate 4.3.x home page.