KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > rift > coad > daemon > timer > db > util > HibernateUtil


1 /*
2  * Timer: The timer class
3  * Copyright (C) 2006-2007 Rift IT Contracting
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * HibernateUtil.java
20  */

21
22 package com.rift.coad.daemon.timer.db.util;
23
24 //coadunation imports
25
import com.rift.coad.daemon.timer.TimerException;
26 import com.rift.coad.lib.configuration.*;
27
28 // hibernate imports
29
import org.hibernate.*;
30 import org.hibernate.cfg.*;
31
32 /**
33  * This class sets up a hibernate SessionFactory.
34  */

35 public class HibernateUtil {
36     
37     private static SessionFactory sessionFactory;
38     
39     /**
40      * Configures up hibernate programmatically using Coadunations configuration
41      * file.
42      */

43     public static void init() throws TimerException {
44         try {
45             // Create the SessionFactory from hibernate.cfg.xml
46
com.rift.coad.lib.configuration.Configuration coadConfig =
47                     com.rift.coad.lib.configuration.ConfigurationFactory.
48                     getInstance()
49                     .getConfig(com.rift.coad.daemon.timer.TimerImpl.class);
50             org.hibernate.cfg.Configuration config = new
51                     org.hibernate.cfg.Configuration()
52             .addResource("com/rift/coad/daemon/timer/db/Schedule.hbm.xml")
53             .setProperty("hibernate.dialect",coadConfig.getString("db_dialect"))
54             .setProperty("hibernate.connection.datasource",
55                     coadConfig.getString("db_datasource"))
56             .setProperty("hibernate.current_session_context_class","jta")
57             .setProperty("hibernate.transaction.factory_class",
58                     "org.hibernate.transaction.JTATransactionFactory")
59             .setProperty("hibernate.transaction.manager_lookup_class",
60                     "org.hibernate.transaction.JOTMTransactionManagerLookup")
61             .setProperty("hibernate.cache.provider_class",
62                     "org.hibernate.cache.NoCacheProvider")
63             .setProperty("hibernate.show_sql",
64                     coadConfig.getString("hibernate_sql","false"))
65             .setProperty("hibernate.hbm2ddl.auto",
66                     coadConfig.getString("hibernate_hbm2ddl_auto","update"));
67             sessionFactory = config.buildSessionFactory();
68         } catch (Throwable JavaDoc ex) {
69             // Make sure you log the exception, as it might be swallowed
70
throw new TimerException("Initial SessionFactory creation failed: "
71                     + ex);
72         }
73     }
74     
75     /**
76      * @return Returns the Hibernate SessionFactory.
77      */

78     public static SessionFactory getSessionFactory() {
79         return sessionFactory;
80     }
81     
82 }
Popular Tags