Hibernate - SessionFactory - II

17 August 2008

Do read the first part of this one before going through.

Now we have the configuration file done, let’s create a helper class that will configure and build the SessionFactory object. This helper will be used in other Hibernate example in this site.

package org.test.example.hibernate.app;
 
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
 
public class SessionFactoryHelper {
    private static final SessionFactory sessionFactory;
 
    static {
        try {            
            /*
             * Build a SessionFactory object from session-factory configuration 
             * defined in the hibernate.cfg.xml file. In this file we register 
             * the JDBC connection information, connection pool, the hibernate 
             * dialect that we used and the mapping to our hbm.xml file for each 
             * POJO (Plain Old Java Object).
             * 
             */
            sessionFactory = new Configuration().configure().buildSessionFactory();
        } catch (Throwable e) {
            System.err.println("Error in creating SessionFactory object." 
                + e.getMessage());
            throw new ExceptionInInitializerError(e);
        }
    }
 
    /*
     * A static method for other application to get SessionFactory object 
     * initialized in this helper class.
     * 
     */
    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}

del.icio.us:Hibernate - SessionFactory - II  digg:Hibernate - SessionFactory - II  spurl:Hibernate - SessionFactory - II  wists:Hibernate - SessionFactory - II  simpy:Hibernate - SessionFactory - II  newsvine:Hibernate - SessionFactory - II  blinklist:Hibernate - SessionFactory - II  furl:Hibernate - SessionFactory - II  reddit:Hibernate - SessionFactory - II  fark:Hibernate - SessionFactory - II  blogmarks:Hibernate - SessionFactory - II  Y!:Hibernate - SessionFactory - II  smarking:Hibernate - SessionFactory - II  magnolia:Hibernate - SessionFactory - II  segnalo:Hibernate - SessionFactory - II  gifttagging:Hibernate - SessionFactory - II

Top Of Page | Trackback

If you found this page useful, consider linking to it. Simply copy and paste the code below into your web site.

It will look like this: Hibernate - SessionFactory - II

Leave a Reply