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().
applySettings(configuration.getProperties());
SessionFactory factory = configuration.buildSessionFactory(builder.build());

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.

Comments

comments

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.

Comments

  1. Your code made my day. Thanks much.

  2. 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();
    configuration.configure();

    ServiceRegistryBuilder builder = new ServiceRegistryBuilder().
    applySettings(configuration.getProperties());
    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 ,java.io.IOException {};
    { save();
    //Search();
    } // response.getWriter().println(“Record saved”);

    //}

    public static void Search()
    {

    Session s=Factory.generate();
    tx = s.beginTransaction();
    Criteria cr = s.createCriteria(Student.class);
    //Criterion salary = Restrictions.gt(“salary”, 2000);
    Criterion name = Restrictions.ilike(“studname”,”zara%”);
    List results = (List) cr.list();
    for(Student one:results)
    {
    if(one.getStudname().equals(“zara”))
    {
    System.out.println(“—————————–”);
    System.out.println(“Searched”+one.getStudname());
    System.out.println(“—————————–”);
    }
    }
    // 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);
    if(s==null)
    {System.out.println(“IT IS NULL”);
    }

    tx = s.beginTransaction();
    // s.createQuery(“FROM Employee E WHERE E.id > 10 ORDER BY E.salary DESC”)
    Student st = new Student();
    st.setStudname(“Ratan Bhau”);
    Address add = new Address();
    add.setAddressname(“pune”);
    st.setAddress(add);
    tx = s.beginTransaction();
    s.save(st);
    tx.commit();
    if(s!=null)
    {
    s.close();
    System.out.println(“Record saved”);
    }

    } catch (Exception e) {
    // TODO: handle exception
    tx.rollback();
    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
    java.lang.NullPointerException
    at com.syntel.test.Test.save(Test.java:83)
    at com.syntel.test.Test.(Test.java:30)
    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(DefaultInstanceManager.java:119)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1062)
    at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:813)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:135)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:395)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:250)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

Speak Your Mind

*