How To Create Session Factory in Hibernate 4

There are many APIs deprecated in the hibernate core framework. One of the frustrating point at this time is, hibernate official documentation is not providing the clear instructions on how to use the new API and it stats that the documentation is in complete. Also each incremental version gets changes on some of the important API. One such thing is the new API for creating the session factory in Hibernate 4.3.0 on wards. In our earlier versions we have created the session factory as below:

SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();

The method buildSessionFactory is deprecated from the hibernate 4 release and it is replaced with the new API. If you are using the hibernate 4.3.0 and above, your code has to be:

Configuration configuration = new Configuration().configure();
StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().
SessionFactory factory = configuration.buildSessionFactory(;

Class ServiceRegistryBuilder is  replaced by StandardServiceRegistryBuilder from 4.3.0. It looks like there will be lot of changes in the 5.0 release. Still there is not much clarity on the deprecated APIs and the suitable alternatives to use. Every incremental release comes up with more deprecated API, they are in way of fine tuning the core framework for the release 5.0.



About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

  • Jaminyah

    Your code made my day. Thanks much.

  • http://s suresh

    PLease help i m getting INFO: Marking servlet Test as unavailable
    And sesssion is null
    public class Factory {
    public static Session generate()

    Configuration configuration=new Configuration();

    ServiceRegistryBuilder builder = new ServiceRegistryBuilder().
    SessionFactory factory = configuration.buildSessionFactory(builder.buildServiceRegistry());
    Session ses=factory.openSession();
    return ses;


    public class Test extends HttpServlet{
    static Transaction tx = null;

    protected void doPost(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse resp) throws javax.servlet.ServletException , {};
    { save();
    } // response.getWriter().println(“Record saved”);


    public static void Search()

    Session s=Factory.generate();
    tx = s.beginTransaction();
    Criteria cr = s.createCriteria(Student.class);
    //Criterion salary =“salary”, 2000);
    Criterion name = Restrictions.ilike(“studname”,”zara%”);
    List results = (List) cr.list();
    for(Student one:results)
    // To get records matching with OR condistions LogicalExpression orExp = Restrictions.or(salary, name); cr.add( orExp ); // To get records matching with AND condistions LogicalExpression andExp = Restrictions.and(salary, name); cr.add( andExp ); List results = cr.list();
    public static void save() {
    try {

    System.out.println(“AT SAVE”);
    Session s = Factory.generate();
    System.out.println(“IT IS “+s);
    {System.out.println(“IT IS NULL”);

    tx = s.beginTransaction();
    // s.createQuery(“FROM Employee E WHERE > 10 ORDER BY E.salary DESC”)
    Student st = new Student();
    st.setStudname(“Ratan Bhau”);
    Address add = new Address();
    tx = s.beginTransaction();;
    System.out.println(“Record saved”);

    } catch (Exception e) {
    // TODO: handle exception
    System.out.println(“” + e.getStackTrace());
    INFO: Marking servlet Test as unavailable
    May 31, 2014 9:30:40 PM org.apache.catalina.core.StandardWrapperValve invoke
    SEVERE: Allocate exception for servlet Test
    at com.syntel.test.Test.(
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at java.lang.Class.newInstance(Unknown Source)
    at org.apache.catalina.core.DefaultInstanceManager.newInstance(
    at org.apache.catalina.core.StandardWrapper.loadServlet(
    at org.apache.catalina.core.StandardWrapper.allocate(
    at org.apache.catalina.core.StandardWrapperValve.invoke(
    at org.apache.catalina.core.StandardContextValve.invoke(
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(
    at org.apache.catalina.core.StandardHostValve.invoke(
    at org.apache.catalina.valves.ErrorReportValve.invoke(
    at org.apache.catalina.valves.AccessLogValve.invoke(
    at org.apache.catalina.core.StandardEngineValve.invoke(
    at org.apache.catalina.connector.CoyoteAdapter.service(
    at org.apache.coyote.http11.Http11Processor.process(
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$ Source)
    at Source)