001 /* 002 Copyright (C) 2001-2003 Lionel Seinturier <Lionel.Seinturier@lip6.fr> 003 004 This program is free software; you can redistribute it and/or modify 005 it under the terms of the GNU Lesser General Public License as 006 published by the Free Software Foundation; either version 2 of the 007 License, or (at your option) any later version. 008 009 This program is distributed in the hope that it will be useful, 010 but WITHOUT ANY WARRANTY; without even the implied warranty of 011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 012 GNU Lesser General Public License for more details. 013 014 You should have received a copy of the GNU Lesser General Public License 015 along with this program; if not, write to the Free Software 016 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ 017 018 package org.objectweb.jac.aspects.hibernate; 019 020 import net.sf.hibernate.HibernateException; 021 import net.sf.hibernate.MappingException; 022 import net.sf.hibernate.Session; 023 import net.sf.hibernate.SessionFactory; 024 import net.sf.hibernate.Transaction; 025 import net.sf.hibernate.cfg.Configuration; 026 import net.sf.hibernate.tool.hbm2ddl.SchemaExport; 027 028 /** 029 * This class acts as a gateway between the AC HibernateAC 030 * and the Hibernate 2.0 framework. 031 * This class manages a singleton instance of itself. 032 * 033 * @author Lionel Seinturier <Lionel.Seinturier@lip6.fr> 034 * @version 1.0 035 */ 036 public class HibernateHelper { 037 038 private static HibernateHelper singleton = new HibernateHelper(); 039 040 /** 041 * @return the singleton instance of this class 042 */ 043 public static HibernateHelper get() { 044 return singleton; 045 } 046 047 /** 048 * The Hibernate configuration. 049 * "Side effect" of creating this object: 050 * the ressource /hibernate.properties is loaded. 051 */ 052 private Configuration cfg = new Configuration(); 053 054 /** 055 * Add a class to the Hibernate configuration. 056 * Given a class named apackage.ClassA, 057 * this triggers the loading of the property /apackage/ClassA..hbm.xml 058 */ 059 public void addClass( Class cl ) throws MappingException { 060 cfg.addClass(cl); 061 rebuildsf = true; 062 } 063 064 /** 065 * Export to the database the table schema for persistent classes. 066 */ 067 public void schemaExport() throws HibernateException { 068 new SchemaExport(cfg).create(false,true); 069 } 070 071 /** 072 * Flag to know whether the configuration has changed, 073 * and if a new SessionFactory has to be constructed. 074 */ 075 private boolean rebuildsf = true; 076 private SessionFactory sf; 077 078 /** 079 * @return the session factory associated to the Hibernate configuration 080 */ 081 private SessionFactory getSessionFactory() throws HibernateException { 082 if (rebuildsf) { 083 sf = cfg.buildSessionFactory(); 084 rebuildsf = false; 085 } 086 return sf; 087 } 088 089 /** 090 * Open an Hibernate session, and start a transaction. 091 * Users call session.save() or session.load() any number of times, 092 * commit or rollback the transaction, 093 * and close the session. 094 */ 095 public void openSessionAndBeginTx() throws HibernateException { 096 SessionFactory sf = getSessionFactory(); 097 session = sf.openSession(); 098 tx = session.beginTransaction(); 099 } 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("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("openSessionAndBeginTx() should have been called first"); 114 return tx; 115 } 116 }