Home Hibernate Hibernate4.3 How to create SessionFactory in Hibernate 4.3.1?

Related Tutorials


 
 

Share on Google+Share on Google+

How to create SessionFactory in Hibernate 4.3.1?

Advertisement
In this tutorial you will lean the new way of creating the SessionFactory in Hibernate 4.3. The Hibernate API is changed in the Hibernate 4.3.x and you should create the SessionFactory in the described here.

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.

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: February 2, 2014

Related Tutorials

Discuss: How to create SessionFactory in Hibernate 4.3.1?  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:1
Tahir Alvi
May 19, 2014
Very well written article on Hibernate 4.3.1

Hi, I found this article after 3 hours search on Google. I found it very useful for my first program in Hibernate 4.3.1. The information provided very clearly and code is clean. Thanks a lot for sharing this content.
DMCA.com