KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > aspects > hibernate > HibernateHelper


1 /*
2   Copyright (C) 2001-2003 Lionel Seinturier <Lionel.Seinturier@lip6.fr>
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2 of the
7   License, or (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12   GNU Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public License
15   along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */

17
18 package org.objectweb.jac.aspects.hibernate;
19
20 import net.sf.hibernate.HibernateException;
21 import net.sf.hibernate.MappingException;
22 import net.sf.hibernate.Session;
23 import net.sf.hibernate.SessionFactory;
24 import net.sf.hibernate.Transaction;
25 import net.sf.hibernate.cfg.Configuration;
26 import net.sf.hibernate.tool.hbm2ddl.SchemaExport;
27
28 /**
29  * This class acts as a gateway between the AC HibernateAC
30  * and the Hibernate 2.0 framework.
31  * This class manages a singleton instance of itself.
32  *
33  * @author Lionel Seinturier <Lionel.Seinturier@lip6.fr>
34  * @version 1.0
35  */

36 public class HibernateHelper {
37     
38     private static HibernateHelper singleton = new HibernateHelper();
39     
40     /**
41      * @return the singleton instance of this class
42      */

43     public static HibernateHelper get() {
44         return singleton;
45     }
46
47     /**
48      * The Hibernate configuration.
49      * "Side effect" of creating this object:
50      * the ressource /hibernate.properties is loaded.
51      */

52     private Configuration cfg = new Configuration();
53     
54     /**
55      * Add a class to the Hibernate configuration.
56      * Given a class named apackage.ClassA,
57      * this triggers the loading of the property /apackage/ClassA..hbm.xml
58      */

59     public void addClass( Class JavaDoc cl ) throws MappingException {
60         cfg.addClass(cl);
61         rebuildsf = true;
62     }
63     
64     /**
65      * Export to the database the table schema for persistent classes.
66      */

67     public void schemaExport() throws HibernateException {
68         new SchemaExport(cfg).create(false,true);
69     }
70     
71     /**
72      * Flag to know whether the configuration has changed,
73      * and if a new SessionFactory has to be constructed.
74      */

75     private boolean rebuildsf = true;
76     private SessionFactory sf;
77     
78     /**
79      * @return the session factory associated to the Hibernate configuration
80      */

81     private SessionFactory getSessionFactory() throws HibernateException {
82         if (rebuildsf) {
83             sf = cfg.buildSessionFactory();
84             rebuildsf = false;
85         }
86         return sf;
87     }
88     
89     /**
90      * Open an Hibernate session, and start a transaction.
91      * Users call session.save() or session.load() any number of times,
92      * commit or rollback the transaction,
93      * and close the session.
94      */

95     public void openSessionAndBeginTx() throws HibernateException {
96         SessionFactory sf = getSessionFactory();
97         session = sf.openSession();
98         tx = session.beginTransaction();
99     }
100     
101     /** The current Hibernate session for saving persistent data. */
102     private Session session;
103     public Session getSession() {
104         if ( session == null || !session.isOpen() )
105             throw new RuntimeException JavaDoc("openSessionAndBeginTx() should have been called first");
106         return session;
107     }
108     
109     /** The current Hibernate transaction for saving persistent data. */
110     private Transaction tx;
111     public Transaction getTx() throws HibernateException {
112         if ( tx == null || tx.wasCommitted() || tx.wasRolledBack() )
113             throw new RuntimeException JavaDoc("openSessionAndBeginTx() should have been called first");
114         return tx;
115     }
116 }
117
Popular Tags