KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pentaho > repository > HibernateUtil


1 /*
2  * Copyright 2006 Pentaho Corporation. All rights reserved.
3  * This software was developed by Pentaho Corporation and is provided under the terms
4  * of the Mozilla Public License, Version 1.1, or any later version. You may not use
5  * this file except in compliance with the license. If you need a copy of the license,
6  * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
7  * BI Platform. The Initial Developer is Pentaho Corporation.
8  *
9  * Software distributed under the Mozilla Public License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
11  * the license for the specific language governing your rights and limitations.
12  *
13  * @created Apr 20, 2005
14  * @author Marc Batchelor
15  *
16  */

17
18 package org.pentaho.repository;
19
20 import java.io.File JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.StringTokenizer JavaDoc;
24 import javax.naming.Context JavaDoc;
25 import javax.naming.InitialContext JavaDoc;
26 import javax.naming.NamingException JavaDoc;
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.dom4j.Node;
30 import org.hibernate.HibernateException;
31 import org.hibernate.Interceptor;
32 import org.hibernate.Query;
33 import org.hibernate.Session;
34 import org.hibernate.SessionFactory;
35 import org.hibernate.Transaction;
36 import org.hibernate.cfg.Configuration;
37 import org.hibernate.cfg.Environment;
38 import org.hibernate.dialect.Dialect;
39 import org.pentaho.core.repository.RepositoryException;
40 import org.pentaho.core.system.IApplicationContext;
41 import org.pentaho.core.system.IPentahoSystemEntryPoint;
42 import org.pentaho.core.system.IPentahoSystemExitPoint;
43 import org.pentaho.core.system.ISystemSettings;
44 import org.pentaho.core.system.PentahoSystem;
45 import org.pentaho.core.util.DatasourceHelper;
46 import org.pentaho.messages.MessageUtil;
47 import org.pentaho.messages.Messages;
48 import org.pentaho.core.repository.content.ContentException;
49
50 public class HibernateUtil implements IPentahoSystemEntryPoint, IPentahoSystemExitPoint{
51
52     private static Log log = LogFactory.getLog(HibernateUtil.class);
53
54     private final static boolean debug = PentahoSystem.debug;
55
56     private static Configuration configuration;
57
58     private static SessionFactory sessionFactory;
59     
60     private static final byte[] lock = new byte[0];
61
62     private static final ThreadLocal JavaDoc threadSession = new ThreadLocal JavaDoc();
63
64     private static final ThreadLocal JavaDoc threadTransaction = new ThreadLocal JavaDoc();
65
66     private static final ThreadLocal JavaDoc threadInterceptor = new ThreadLocal JavaDoc();
67
68     // private static final ThreadLocal commitNeeded = new ThreadLocal();
69
private static boolean hibernateManaged;
70
71     private static String JavaDoc factoryJndiName;
72
73     private static Context JavaDoc iniCtx;
74
75     private static List JavaDoc objectHandlers = new ArrayList JavaDoc();
76
77     private static final String JavaDoc QUERYWILDCARD = "%{0}%"; //$NON-NLS-1$
78

79     static {
80         // JIRA case #PLATFORM 150: removed listener and changed to lazy init
81
initialize();
82     }
83
84     protected static boolean initialize() {
85         IApplicationContext applicationContext = PentahoSystem.getApplicationContext();
86         // Add to entry/exit points list
87
HibernateUtil hUtil = new HibernateUtil();
88         applicationContext.addEntryPointHandler(hUtil);
89         applicationContext.addExitPointHandler(hUtil);
90         
91         // Look for some hibernate-specific properties...
92
String JavaDoc hibernateConfigurationFile = applicationContext.getProperty("hibernateConfigPath", null); //$NON-NLS-1$
93
if ((hibernateConfigurationFile != null)) {
94             if (hibernateConfigurationFile.trim().length() == 0) {
95                 hibernateConfigurationFile = null;
96             }
97         }
98         String JavaDoc tmp = applicationContext.getProperty("hibernateManaged", "false").trim().toLowerCase(); //$NON-NLS-1$ //$NON-NLS-2$
99
hibernateManaged = tmp.equals("true"); //$NON-NLS-1$
100
try {
101             configuration = new Configuration();
102             configuration.setEntityResolver(new PentahoEntityResolver());
103             configuration.setListener("load", new HibernateLoadEventListener()); //$NON-NLS-1$
104

105             if (hibernateConfigurationFile != null) {
106                 String JavaDoc configPath = applicationContext.getSolutionPath(hibernateConfigurationFile);
107                 File JavaDoc cfgFile = new File JavaDoc(configPath);
108                 if (cfgFile.exists()) {
109                     configuration.configure(cfgFile);
110                 } else {
111                     log.error(Messages.getErrorString("HIBUTIL.ERROR_0012_CONFIG_NOT_FOUND", configPath)); //$NON-NLS-1$
112
return false;
113                 }
114             } else {
115                 configuration.configure(); // Read in the .xml document
116
}
117             String JavaDoc dsName = configuration.getProperty("connection.datasource"); //$NON-NLS-1$
118
if ((dsName != null) && dsName.toUpperCase().endsWith("HIBERNATE")) { //$NON-NLS-1$
119
String JavaDoc actualDSName = DatasourceHelper.getDSBoundName("Hibernate"); //$NON-NLS-1$
120
configuration.setProperty("hibernate.connection.datasource", actualDSName); //$NON-NLS-1$
121
}
122
123             setupConfigurationHandlers();
124             // Add in the classes we know about.
125
addConfigurations(configuration);
126
127             /*
128              * configuration.addResource("org/pentaho/repository/runtime/RuntimeElement.hbm.xml");
129              * //$NON-NLS-1$
130              * configuration.addResource("org/pentaho/repository/content/ContentLocation.hbm.xml");//$NON-NLS-1$
131              * configuration.addResource("org/pentaho/repository/content/ContentItem.hbm.xml");//$NON-NLS-1$
132              * configuration.addResource("org/pentaho/repository/content/ContentItemFile.hbm.xml");//$NON-NLS-1$
133              * configuration.addResource("org/pentaho/repository/DefinitionVersionManager.hbm.xml");//$NON-NLS-1$
134              */

135             if (!hibernateManaged) {
136                 log.info(Messages.getString("HIBUTIL.USER_HIBERNATEUNMANAGED")); //$NON-NLS-1$
137
sessionFactory = configuration.buildSessionFactory();
138             } else {
139                 factoryJndiName = configuration.getProperty(Environment.SESSION_FACTORY_NAME);
140                 if (factoryJndiName == null) {
141                     log.error(Messages.getErrorString("HIBUTIL.ERROR_0013_NO_SESSION_FACTORY"));//$NON-NLS-1$
142
return false;
143                 }
144                 log.info(Messages.getString("HIBUTIL.USER_HIBERNATEMANAGED")); //$NON-NLS-1$
145
configuration.buildSessionFactory(); // Let hibernate Bind it
146
// to JNDI...
147
}
148             Dialect.getDialect(configuration.getProperties());
149             DefinitionVersionManager.performAutoUpdateIfRequired();
150             return true;
151         } catch (Throwable JavaDoc ex) {
152             log.error(Messages.getErrorString("HIBUTIL.ERROR_0006_BUILD_SESSION_FACTORY"), ex); //$NON-NLS-1$
153
throw new ExceptionInInitializerError JavaDoc(ex);
154         }
155     }
156
157     private static void setupConfigurationHandlers() {
158         ISystemSettings systemSettings = PentahoSystem.getSystemSettings();
159         List JavaDoc objectHandlerDefs = systemSettings.getSystemSettings("HibernatedObjectHandlers/*"); //$NON-NLS-1$
160
if ((objectHandlerDefs != null) && (objectHandlerDefs.size() > 0)) {
161             String JavaDoc handlerClass;
162             for (int i = 0; i < objectHandlerDefs.size(); i++) {
163                 handlerClass = ((Node) objectHandlerDefs.get(i)).getText();
164                 IHibernatedObjectExtensionList extension = (IHibernatedObjectExtensionList) PentahoSystem.createObject(handlerClass);
165                 if (extension != null) {
166                     objectHandlers.add(extension);
167                 }
168             }
169         } else {
170             // Handle old condition where it didn't exist in pentaho.xml
171
objectHandlers.add(new StdHibernateClassHandler());
172         }
173     }
174
175     private static void addConfigurations(Configuration theConfiguration) {
176         IHibernatedObjectExtensionList extension;
177         for (int i = 0; i < objectHandlers.size(); i++) {
178             extension = (IHibernatedObjectExtensionList) objectHandlers.get(i);
179             List JavaDoc resourceList = extension.getHibernatedObjectResourceList();
180             for (int j = 0; j < resourceList.size(); j++) {
181                 theConfiguration.addResource((String JavaDoc) resourceList.get(j));
182             }
183         }
184     }
185
186     /**
187      * Returns the SessionFactory used for this static class.
188      *
189      * @return SessionFactory
190      */

191     public static SessionFactory getSessionFactory() {
192         if (!hibernateManaged) {
193             return sessionFactory;
194         }
195         SessionFactory sf = null;
196         try {
197             if (iniCtx == null) {
198                 iniCtx = new InitialContext JavaDoc();
199             }
200             String JavaDoc jndiName = factoryJndiName;
201             try {
202                 sf = (SessionFactory) iniCtx.lookup(jndiName);
203             } catch (Exception JavaDoc ignored) {
204             }
205             if (sf == null) {
206                 try {
207                     sf = (SessionFactory) iniCtx.lookup("java:" + jndiName); //$NON-NLS-1$
208
} catch (Exception JavaDoc ignored) {
209                     ignored.printStackTrace();
210                 }
211             }
212         } catch (NamingException JavaDoc ignored) {
213         }
214         return sf;
215     }
216
217     /**
218      * Returns the original Hibernate configuration.
219      *
220      * @return Configuration
221      */

222     public static Configuration getConfiguration() {
223         return configuration;
224     }
225
226     public static void updateSchema() throws RepositoryException {
227         PentahoSchemaUpdate upd = new PentahoSchemaUpdate(getConfiguration());
228         upd.execute(true, true);
229     }
230
231     /**
232      * Rebuild the SessionFactory with the static Configuration.
233      *
234      */

235     public static void rebuildSessionFactory() throws RepositoryException {
236         if (!hibernateManaged) {
237             synchronized (lock) {
238                 try {
239                     sessionFactory = getConfiguration().buildSessionFactory();
240                 } catch (Exception JavaDoc ex) {
241                     log.error(Messages.getErrorString("HIBUTIL.ERROR_0007_REBUILD_SESSION_FACTORY"), ex); //$NON-NLS-1$
242
throw new RepositoryException(Messages.getErrorString("HIBUTIL.ERROR_0007_REBUILD_SESSION_FACTORY"), ex); //$NON-NLS-1$
243
}
244             }
245         } else {
246             try {
247                 getConfiguration().buildSessionFactory();
248             } catch (Exception JavaDoc ex) {
249                 log.error(Messages.getErrorString("HIBUTIL.ERROR_0007_REBUILD_SESSION_FACTORY"), ex); //$NON-NLS-1$
250
throw new RepositoryException(Messages.getErrorString("HIBUTIL.ERROR_0007_REBUILD_SESSION_FACTORY"), ex); //$NON-NLS-1$
251
}
252         }
253     }
254
255     /**
256      * Rebuild the SessionFactory with the given Hibernate Configuration.
257      *
258      * @param cfg
259      */

260     public static void rebuildSessionFactory(Configuration cfg) throws RepositoryException {
261         if (!hibernateManaged) {
262             synchronized (lock) {
263                 try {
264                     sessionFactory = cfg.buildSessionFactory();
265                     configuration = cfg;
266                 } catch (Exception JavaDoc ex) {
267                     log.error(Messages.getErrorString("HIBUTIL.ERROR_0007_REBUILD_SESSION_FACTORY"), ex); //$NON-NLS-1$
268
throw new RepositoryException(Messages.getErrorString("HIBUTIL.ERROR_0007_REBUILD_SESSION_FACTORY"), ex); //$NON-NLS-1$
269
}
270             }
271         } else {
272             try {
273                 cfg.buildSessionFactory();
274                 configuration = cfg;
275             } catch (Exception JavaDoc ex) {
276                 log.error(Messages.getErrorString("HIBUTIL.ERROR_0007_REBUILD_SESSION_FACTORY"), ex); //$NON-NLS-1$
277
throw new RepositoryException(Messages.getErrorString("HIBUTIL.ERROR_0007_REBUILD_SESSION_FACTORY"), ex); //$NON-NLS-1$
278
}
279         }
280     }
281
282     /**
283      * Retrieves the current Session local to the thread. <p/> If no Session is
284      * open, opens a new Session for the running thread.
285      *
286      * @return Session
287      */

288     public static Session getSession() throws RepositoryException {
289         Session s = (Session) threadSession.get();
290         try {
291             if (s == null) {
292                 if (debug)
293                     log.debug(Messages.getString("HIBUTIL.DEBUG_OPEN_NEW_SESSION")); //$NON-NLS-1$
294
if (getInterceptor() != null) {
295                     if (debug)
296                         log.debug(Messages.getString("HIBUTIL.DEBUG_USING_INTERCEPTOR") + getInterceptor().getClass()); //$NON-NLS-1$
297
s = getSessionFactory().openSession(getInterceptor());
298                 } else {
299                     s = getSessionFactory().openSession();
300                 }
301                 threadSession.set(s);
302             }
303         } catch (HibernateException ex) {
304             log.error(Messages.getErrorString("HIBUTIL.ERROR_0005_GET_SESSION"), ex); //$NON-NLS-1$
305
throw new RepositoryException(Messages.getErrorString("HIBUTIL.ERROR_0005_GET_SESSION"), ex); //$NON-NLS-1$
306
}
307         return s;
308     }
309
310     public static void flushSession() throws RepositoryException {
311         try {
312             Session s = getSession();
313             s.flush();
314         } catch (HibernateException ex) {
315             throw new RepositoryException(ex);
316         }
317     }
318
319     /**
320      * Closes the Session local to the thread.
321      */

322     public static void closeSession() throws RepositoryException {
323         try {
324             Session s = (Session) threadSession.get();
325             threadSession.set(null);
326             if (s != null && s.isOpen()) {
327                 if (debug)
328                     log.debug(Messages.getString("HIBUTIL.DEBUG_CLOSING_SESSION")); //$NON-NLS-1$
329
s.close();
330             }
331             threadTransaction.set(null);
332         } catch (HibernateException ex) {
333             log.error(Messages.getErrorString("HIBUTIL.ERROR_0009_CLOSE_SESSION"), ex); //$NON-NLS-1$
334
threadTransaction.set(null);
335             throw new RepositoryException(Messages.getErrorString("HIBUTIL.ERROR_0009_CLOSE_SESSION"), ex); //$NON-NLS-1$
336
}
337
338     }
339
340     /**
341      * Start a new database transaction.
342      */

343     public static void beginTransaction() throws RepositoryException {
344         // commitNeeded.set(Boolean.TRUE);
345
Transaction tx = (Transaction) threadTransaction.get();
346         try {
347             if (tx == null) {
348                 if (debug)
349                     log.debug(Messages.getString("HIBUTIL.DEBUG_START_TRANS")); //$NON-NLS-1$
350
tx = getSession().beginTransaction();
351                 threadTransaction.set(tx);
352             }
353         } catch (HibernateException ex) {
354             log.error(Messages.getErrorString("HIBUTIL.ERROR_0004_START_TRANS"), ex); //$NON-NLS-1$
355
throw new RepositoryException(Messages.getErrorString("HIBUTIL.ERROR_0004_START_TRANS"), ex); //$NON-NLS-1$
356
}
357     }
358
359     /**
360      * Commit the database transaction.
361      */

362     public static void commitTransaction() throws RepositoryException {
363         // Boolean needed = (Boolean)commitNeeded.get();
364
// if (needed.booleanValue()){
365
Transaction tx = (Transaction) threadTransaction.get();
366         try {
367             if (tx != null && !tx.wasCommitted() && !tx.wasRolledBack()) {
368                 if (debug)
369                     log.debug(Messages.getString("HIBUTIL.DEBUG_COMMIT_TRANS")); //$NON-NLS-1$
370
tx.commit();
371             }
372         } catch (HibernateException ex) {
373             log.error(Messages.getErrorString("HIBUTIL.ERROR_0008_COMMIT_TRANS"), ex); //$NON-NLS-1$
374
try {
375                 rollbackTransaction();
376             } catch (Exception JavaDoc e2) {
377             }
378             // throw new
379
// RepositoryException(Messages.getErrorString("HIBUTIL.ERROR_0008_COMMIT_TRANS"),
380
// ex); //$NON-NLS-1$
381
} finally {
382             threadTransaction.set(null);
383         }
384         // }
385
// commitNeeded.set(Boolean.FALSE);
386
}
387
388     /**
389      * Commit the database transaction.
390      */

391     public static void rollbackTransaction() throws RepositoryException {
392         Transaction tx = (Transaction) threadTransaction.get();
393         try {
394             threadTransaction.set(null);
395             if (tx != null && !tx.wasCommitted() && !tx.wasRolledBack()) {
396                 if (debug)
397                     log.debug(Messages.getString("HIBUTIL.DEBUG_ROLLBACK")); //$NON-NLS-1$
398
tx.rollback();
399             }
400         } catch (HibernateException ex) {
401             log.error(Messages.getErrorString("HIBUTIL.ERROR_0003_ROLLBACK"), ex); //$NON-NLS-1$
402
throw new RepositoryException(Messages.getErrorString("HIBUTIL.ERROR_0003_ROLLBACK"), ex); //$NON-NLS-1$
403
} finally {
404             closeSession();
405         }
406     }
407
408     /**
409      * Reconnects a Hibernate Session to the current Thread.
410      *
411      * @param session
412      * The Hibernate Session to be reconnected.
413      */

414     /*
415      * public static void reconnect(Session session) throws RepositoryException {
416      * try { session.reconnect(); threadSession.set(session); } catch
417      * (HibernateException ex) {
418      * log.error(Messages.getErrorString("HIBUTIL.ERROR_0001_RECONNECT"), ex);
419      * //$NON-NLS-1$ throw new RepositoryException(ex); } }
420      */

421     /**
422      * Disconnect and return Session from current Thread.
423      *
424      * @return Session the disconnected Session
425      */

426     public static Session disconnectSession() throws RepositoryException {
427
428         Session session = getSession();
429         try {
430             threadSession.set(null);
431             if (session.isConnected() && session.isOpen())
432                 session.disconnect();
433         } catch (HibernateException ex) {
434             log.error(Messages.getErrorString("HIBUTIL.ERROR_0002_DISCONNECT"), ex); //$NON-NLS-1$
435
throw new RepositoryException(Messages.getErrorString("HIBUTIL.ERROR_0002_DISCONNECT"), ex); //$NON-NLS-1$
436
}
437         return session;
438     }
439
440     /**
441      * Register a Hibernate interceptor with the current thread.
442      * <p>
443      * Every Session opened is opened with this interceptor after registration.
444      * Has no effect if the current Session of the thread is already open,
445      * effective on next close()/getSession().
446      */

447     public static void registerInterceptor(Interceptor interceptor) {
448         threadInterceptor.set(interceptor);
449     }
450
451     private static Interceptor getInterceptor() {
452         Interceptor interceptor = (Interceptor) threadInterceptor.get();
453         return interceptor;
454     }
455
456     /**
457      * Searches an ISearchable object for a search term. The search rules are as
458      * follows:
459      *
460      * If the searchType is ISearchable.SEARCH_TYPE_PHRASE, then the fields in
461      * the table are searched for the exact phrase given.
462      *
463      * If the searchType is ISearchable.SEARCH_TYPE_WORDS_AND or ..._OR, then
464      * the following happens: a- Each word in the searchTerm is extracted and
465      * put into a list of search terms. b- Each search term is surrounded by the
466      * SQL wildcard '%'. So each search term becomes %term%. c- A dynamic query
467      * is generated searching each of the columns for each search term d- The
468      * searchType is used to determine the connector between each search term.
469      * e- The AND will match only if all of the terms appear in a specific
470      * column - cross-column searching using ..._AND will NOT work. In other
471      * words, if your search term is "East Sales", and your search type is
472      * ..._AND, a row will be returned if one of the columns contains East and
473      * the same column contains Sales. A row will NOT be returned if one column
474      * only contains East, and another column only contains Sales. This type of
475      * functionality could be obtained using a view that concatenates all of the
476      * searchable columns together into one large column, but this would be
477      * costly and database-specific.
478      *
479      * @param searchable
480      * ISearchable to search
481      * @param searchTerm
482      * Search Term - see above for rules
483      * @param searchType
484      * One of:
485      * ISearchable.SEARCH_TYPE_PHRASE,ISearchable.SEARCH_TYPE_WORDS_AND,
486      * ISearchable.SEARCH_TYPE_WORDS_OR
487      * @return A list of objects from Hibernate that met the conditions
488      * specified.
489      */

490     public static List JavaDoc searchForTerm(ISearchable searchable, String JavaDoc searchTerm, int searchType) {
491         Session session = getSession();
492         if (searchType == ISearchable.SEARCH_TYPE_PHRASE) {
493             Query qry = session.getNamedQuery(searchable.getPhraseSearchQueryName());
494             String JavaDoc searchWildcard = MessageUtil.formatErrorMessage(QUERYWILDCARD, searchTerm);
495             qry.setString("searchTerm", searchWildcard); //$NON-NLS-1$
496
List JavaDoc rtn = qry.list();
497             return rtn;
498         }
499         String JavaDoc connector;
500         if (searchType == ISearchable.SEARCH_TYPE_WORDS_AND) {
501             connector = " and "; //$NON-NLS-1$
502
} else {
503             connector = " or "; //$NON-NLS-1$
504
}
505         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(searchTerm, " "); //$NON-NLS-1$
506
List JavaDoc searchWords = new ArrayList JavaDoc();
507         while (st.hasMoreTokens()) {
508             searchWords.add(MessageUtil.formatErrorMessage(QUERYWILDCARD, st.nextToken()));
509         }
510         // Ok, we now have a list of search words.
511
StringBuffer JavaDoc assembly = assembleQuery(searchable.getSearchableTable(), connector, searchWords, searchable.getSearchableColumns());
512         Query qry = session.createQuery(assembly.toString());
513         for (int j = 0; j < searchWords.size(); j++) {
514             qry.setParameter("searchTerm" + j, searchWords.get(j)); //$NON-NLS-1$
515
}
516         List JavaDoc rtn = qry.list();
517         return rtn;
518     }
519
520     private static StringBuffer JavaDoc assembleQuery(String JavaDoc tableName, String JavaDoc connector, List JavaDoc terms, String JavaDoc[] columns) {
521         StringBuffer JavaDoc qry = new StringBuffer JavaDoc();
522         qry.append("from ").append(tableName).append(" tbl where "); //$NON-NLS-1$ //$NON-NLS-2$
523
String JavaDoc currCol, term;
524         for (int colno = 0; colno < columns.length; colno++) {
525             currCol = columns[colno];
526             qry.append("("); //$NON-NLS-1$
527
for (int termNo = 0; termNo < terms.size(); termNo++) {
528                 term = (String JavaDoc) terms.get(termNo);
529                 qry.append("tbl.").append(currCol).append(" like :searchTerm").append(term).append(" "); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
530
if (termNo < terms.size() - 1) {
531                     qry.append(connector);
532                 }
533             }
534             qry.append(")"); //$NON-NLS-1$
535
if (colno < columns.length - 1) {
536                 qry.append(" or "); // Columns are always or'd //$NON-NLS-1$
537
}
538         }
539         return qry;
540     }
541
542     public static void clear() {
543         getSession().clear();
544     }
545
546     /**
547      * Persists changes to the object. Object must be defined to hibernate.
548      *
549      * @param obj
550      * The object to make persistent
551      * @throws RepositoryException
552      */

553     public static void makePersistent(Object JavaDoc obj) throws RepositoryException {
554         if (debug)
555             log.debug(Messages.getString("HIBUTIL.DEBUG_MAKE_PERSISTENT", obj.toString())); //$NON-NLS-1$
556
try {
557             getSession().saveOrUpdate(obj);
558         } catch (HibernateException ex) {
559             log.error(Messages.getErrorString("HIBUTIL.ERROR_0010_SAVING_UPDATING"), ex); //$NON-NLS-1$
560
throw new ContentException(Messages.getErrorString("HIBUTIL.ERROR_0010_SAVING_UPDATING"), ex); //$NON-NLS-1$
561
}
562     }
563
564     /**
565      * Deletes the object from Hibernate
566      *
567      * @param obj
568      * The object to make transient
569      * @throws RepositoryException
570      */

571     public static void makeTransient(Object JavaDoc obj) throws RepositoryException {
572         if (debug)
573             log.debug(Messages.getString("HIBUTIL.DEBUG_MAKE_TRANSIENT", obj.toString())); //$NON-NLS-1$
574
try {
575             getSession().delete(obj);
576         } catch (HibernateException ex) {
577             log.error(Messages.getErrorString("HIBUTIL.ERROR_0011_DELETING_OBJ"), ex); //$NON-NLS-1$
578
throw new ContentException(Messages.getErrorString("HIBUTIL.ERROR_0011_DELETING_OBJ"), ex); //$NON-NLS-1$
579
}
580     }
581
582     /**
583      * Evicts the object from the Hibernate cache. Call this if you don't
584      * believe you'll need this object in the cache. This is also good to call
585      * if you're doing semi-mass updates.
586      *
587      * @param obj
588      */

589     public static void evict(Object JavaDoc obj) {
590         // if (debug)
591
// log.debug(Messages.getString("HIBUTIL.DEBUG_EVICT", obj.toString())); //$NON-NLS-1$
592
try {
593             getSession().evict(obj);
594         } catch (HibernateException ex) {
595             log.error(Messages.getErrorString("HIBUTIL.ERROR_0014_EVICTING_OBJECT"), ex); //$NON-NLS-1$
596
}
597
598     }
599     
600     public static List JavaDoc getHibernatedObjectHandlerList() {
601         return objectHandlers;
602     }
603
604     public void systemEntryPoint() {
605       // No need to do anything for Hibernate here.
606
}
607     
608     public void systemExitPoint() {
609       try {
610           HibernateUtil.commitTransaction();
611       } catch (Throwable JavaDoc t) {
612           t.printStackTrace();
613       }
614
615       try {
616           HibernateUtil.closeSession();
617       } catch (Throwable JavaDoc t) {
618           t.printStackTrace();
619       }
620   }
621
622 }
623
Popular Tags