How to create a sessionfactory in hibernate 4.3.6?

How to create sessionfactory in hibernate 4.3.6?

How to create a sessionfactory in hibernate 4.3.6?

Hibernate is a ORM (Object Relation mapping) framework and creating a sessionfactory is very basic one to start with hibernate.

Before creating sessionfactory, it is very important to know and understand what is sessionfactory ?

If you are new to hibernate then you may not knowing this, but if you have worked with hibernate already, then sessionfactory is the familiar repeated word in hibernate.

that’s fine, let’s understand what is sessionfactory.

Sessionfactory:

Sessionfactory is a hibernate object which maintains datasource, mappings, hibernate configuration information’s etc.,

Sessionfactory is a important interface in hibernate and it’s helps to get the session objects for database CRUD(create, read, update and delete) operations.

sessionfactory will be implemented as singleton design pattern for better performance. Singleton sessionfactory is nothing but one instance per the datasource. If you are creating more sessionfactory instances in your application, then application will become heavy weight, since all the sessionfactories will be having the datasource, mapping and hibernate configuration details individually. sessionfactory is also immutable and it will be created as singleton while the server initializes itself.
Since sessionfactory implemented as singleton, it is thread safe too. But session is not thread safe and it is per client/thread.

Hope you got enough idea about sessionfactory to proceed further.

Let’s create a sessionfactory in hibernate 4.3.6.

[java]

package in.javadomain;

import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

public class HibernateConfig {
private static SessionFactory sessionFactory;

// Uncomment this if you are planning to use way 2 to create session factory
private static ServiceRegistry serviceRegistry;

static {
try {
Configuration configuration = new Configuration().configure();

// way 1
// deprecated in 4/4.3+
// sessionFactory = configuration.buildSessionFactory();

// way 2 recommended for 4.1.8 to 4.3
// serviceRegistry = new
// ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServic//eRegistry();
// sessionFactory =
// configuration.buildSessionFactory(serviceRegistry);

// way 3 – recommended for 4.3.6+
StandardServiceRegistryBuilder standardSrvcRgstryBuilder = new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties());
sessionFactory = configuration.buildSessionFactory(standardSrvcRgstryBuilder.build());

} catch (HibernateException e) {
System.out.println("Problem while creating a sessionfactory!");
}
}

public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
       } catch (HibernateException e) {
            System.out.println("Problem while creating a sessionfactory!");
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}

[/java]

we can create the sessionfactory directly using buildsessionfactory() method. But it is deprecated in the 4/4.3+ versions, so it is advicable to create and make use of the sessionfactory using ServiceRegistry.

ServiceRegistry is basically a additional layer for abstraction. And you can create the sessionfactory using ServiceRegistryBuilder like this, remember ServiceRegistryBuilder is deprecated in 4.3.6

[java]
serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties())
                    .buildServiceRegistry();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
[/java]

 

What will happen when this line executes ?

[java]
Configuration configuration = new Configuration().configure();
[/java]

now all the configurations information’s like database, username, password and other configurations details are loaded in the configuration after executing it.

As discussed above,
we are using the same configuration properties in the additional abstraction layer service registry to create the session factory like below

[java]
serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties())
                    .buildServiceRegistry();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
[/java]

 

ServiceRegistryBuilder is deprecated in 4.3.6+, so we are creating the sessionfactory using “StandardServiceRegistryBuilder” like this,

 

[java]

StandardServiceRegistryBuilder standardSrvcRgstryBuilder = new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties());
sessionFactory = configuration.buildSessionFactory(standardSrvcRgstryBuilder.build());

[/java]

 

Caused by: java.lang.ClassNotFoundException: javax.persistence.NamedStoredProcedureQuery:

You will get this error when you do not have hibernate-jpa-2.1-api Jar. NamedStoredProcedureQuery is the class added to the hibernate jpa-2.1, so we need to have this jar, if we are creating a sessionfactory in hibernate 4.3.6+.

 

You can download this Jar from here,

Hiberante-jpa-2.1-api jar

 

 

Hope you are clear.

Feel free to share your feedbacks/comments/suggestions in the below comment section.

Leave a Reply