KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > ce > auction > persistence > HibernateUtilSimple


1 package org.hibernate.ce.auction.persistence;
2
3 import org.hibernate.*;
4 import org.hibernate.cfg.Configuration;
5 import org.apache.commons.logging.*;
6
7 /**
8  * A very simple Hibernate helper class that holds the SessionFactory as a singleton.
9  * <p>
10  * The only job of this helper class is to give your application code easy
11  * access to the <tt>SessionFactory</tt>. It initializes the <tt>SessionFactory</tt>
12  * when it is loaded (static initializer) and you can easily open new
13  * <tt>Session</tt>s. Only really useful for trivial applications.
14  *
15  * @author christian@hibernate.org
16  */

17 public class HibernateUtilSimple {
18
19     private static Log log = LogFactory.getLog(HibernateUtil.class);
20
21     private static final SessionFactory sessionFactory;
22
23     // Create the initial SessionFactory from the default configuration files
24
static {
25         try {
26             sessionFactory = new Configuration().configure().buildSessionFactory();
27         } catch (Throwable JavaDoc ex) {
28             // We have to catch Throwable, otherwise we will miss
29
// NoClassDefFoundError and other subclasses of Error
30
log.error("Building SessionFactory failed.", ex);
31             throw new ExceptionInInitializerError JavaDoc(ex);
32         }
33     }
34
35     public static Session getSession()
36         throws HibernateException {
37         return sessionFactory.openSession();
38     }
39 }
40
Popular Tags