KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > entity > TransactionScopedHibernateSession


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.ejb3.entity;
23
24 import java.io.Externalizable JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.ObjectInput JavaDoc;
27 import java.io.ObjectOutput JavaDoc;
28 import java.io.Serializable JavaDoc;
29 import java.sql.Connection JavaDoc;
30 import javax.persistence.EntityManager;
31 import org.hibernate.CacheMode;
32 import org.hibernate.Criteria;
33 import org.hibernate.EntityMode;
34 import org.hibernate.Filter;
35 import org.hibernate.FlushMode;
36 import org.hibernate.HibernateException;
37 import org.hibernate.LockMode;
38 import org.hibernate.ReplicationMode;
39 import org.hibernate.SQLQuery;
40 import org.hibernate.Session;
41 import org.hibernate.SessionFactory;
42 import org.hibernate.Transaction;
43 import org.hibernate.ejb.HibernateEntityManager;
44 import org.hibernate.stat.SessionStatistics;
45 import org.jboss.ejb3.PersistenceUnitRegistry;
46
47
48 /**
49  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
50  * @version $Revision: 42263 $
51  */

52 public class TransactionScopedHibernateSession implements Session, Externalizable JavaDoc
53 {
54    private transient ManagedEntityManagerFactory factory;
55
56    protected Session getHibernateSession()
57    {
58       if (getSession() instanceof HibernateEntityManager)
59       {
60          return ((HibernateEntityManager) getSession()).getSession();
61       }
62       throw new RuntimeException JavaDoc("ILLEGAL ACTION: Not a Hibernate pe" +
63                                  "rsistence provider");
64    }
65
66    protected EntityManager getSession()
67    {
68       return factory.getTransactionScopedEntityManager();
69    }
70
71    public TransactionScopedHibernateSession(ManagedEntityManagerFactory factory)
72    {
73       if (factory == null) throw new NullPointerException JavaDoc("factory must not be null");
74       this.factory = factory;
75    }
76
77    public TransactionScopedHibernateSession()
78    {
79
80    }
81
82    public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc
83    {
84       out.writeUTF(factory.getKernelName());
85    }
86
87    public void readExternal(ObjectInput JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc
88    {
89       String JavaDoc kernelName = in.readUTF();
90       PersistenceUnitDeployment deployment = PersistenceUnitRegistry.getPersistenceUnit(kernelName);
91       if (deployment == null) throw new IOException JavaDoc("Unable to find persistence unit in registry: " + kernelName);
92       factory = deployment.getManagedFactory();
93    }
94
95    public EntityMode getEntityMode()
96    {
97       return getHibernateSession().getEntityMode();
98    }
99
100    public Session getSession(EntityMode entityMode)
101    {
102       return getHibernateSession().getSession(entityMode);
103    }
104
105    public void flush()
106    throws HibernateException
107    {
108       getHibernateSession().flush();
109    }
110
111    public void setFlushMode(FlushMode flushMode)
112    {
113       getHibernateSession().setFlushMode(flushMode);
114    }
115
116    public FlushMode getFlushMode()
117    {
118       return getHibernateSession().getFlushMode();
119    }
120
121    public void setCacheMode(CacheMode cacheMode)
122    {
123       getHibernateSession().setCacheMode(cacheMode);
124    }
125
126    public CacheMode getCacheMode()
127    {
128       return getHibernateSession().getCacheMode();
129    }
130
131    public SessionFactory getSessionFactory()
132    {
133       return getHibernateSession().getSessionFactory();
134    }
135
136    public Connection JavaDoc connection()
137    throws HibernateException
138    {
139       return getHibernateSession().connection();
140    }
141
142    public Connection JavaDoc disconnect()
143    throws HibernateException
144    {
145       return getHibernateSession().disconnect();
146    }
147
148    public void reconnect()
149    throws HibernateException
150    {
151       getHibernateSession().reconnect();
152    }
153
154    public void reconnect(Connection JavaDoc connection)
155    throws HibernateException
156    {
157       getHibernateSession().reconnect(connection);
158    }
159
160    public Connection JavaDoc close()
161    throws HibernateException
162    {
163       throw new IllegalStateException JavaDoc("Illegal to call this method from injected, managed EntityManager");
164    }
165
166    public void cancelQuery()
167    throws HibernateException
168    {
169       getHibernateSession().cancelQuery();
170    }
171
172    public boolean isOpen()
173    {
174       return getHibernateSession().isOpen();
175    }
176
177    public boolean isConnected()
178    {
179       return getHibernateSession().isConnected();
180    }
181
182    public boolean isDirty()
183    throws HibernateException
184    {
185       return getHibernateSession().isDirty();
186    }
187
188    public Serializable JavaDoc getIdentifier(Object JavaDoc object)
189    throws HibernateException
190    {
191       return getHibernateSession().getIdentifier(object);
192    }
193
194    public boolean contains(Object JavaDoc object)
195    {
196       return getHibernateSession().contains(object);
197    }
198
199    public void evict(Object JavaDoc object)
200    throws HibernateException
201    {
202       getHibernateSession().evict(object);
203    }
204
205    public Object JavaDoc load(Class JavaDoc theClass, Serializable JavaDoc id, LockMode lockMode)
206    throws HibernateException
207    {
208       return getHibernateSession().load(theClass, id, lockMode);
209    }
210
211    public Object JavaDoc load(String JavaDoc entityName, Serializable JavaDoc id, LockMode lockMode)
212    throws HibernateException
213    {
214       return getHibernateSession().load(entityName, id, lockMode);
215    }
216
217    public Object JavaDoc load(Class JavaDoc theClass, Serializable JavaDoc id)
218    throws HibernateException
219    {
220       return getHibernateSession().load(theClass, id);
221    }
222
223    public Object JavaDoc load(String JavaDoc entityName, Serializable JavaDoc id)
224    throws HibernateException
225    {
226       return getHibernateSession().load(entityName, id);
227    }
228
229    public void load(Object JavaDoc object, Serializable JavaDoc id)
230    throws HibernateException
231    {
232       getHibernateSession().load(object, id);
233    }
234
235    public void replicate(Object JavaDoc object, ReplicationMode replicationMode)
236    throws HibernateException
237    {
238       getHibernateSession().replicate(object, replicationMode);
239    }
240
241    public void replicate(String JavaDoc entityName, Object JavaDoc object, ReplicationMode replicationMode)
242    throws HibernateException
243    {
244       getHibernateSession().replicate(entityName, object, replicationMode);
245    }
246
247    public Serializable JavaDoc save(Object JavaDoc object)
248    throws HibernateException
249    {
250       return getHibernateSession().save(object);
251    }
252
253    public Serializable JavaDoc save(String JavaDoc entityName, Object JavaDoc object)
254    throws HibernateException
255    {
256       return getHibernateSession().save(entityName, object);
257    }
258
259    public void saveOrUpdate(Object JavaDoc object)
260    throws HibernateException
261    {
262       getHibernateSession().saveOrUpdate(object);
263    }
264
265    public void saveOrUpdate(String JavaDoc entityName, Object JavaDoc object)
266    throws HibernateException
267    {
268       getHibernateSession().saveOrUpdate(entityName, object);
269    }
270
271    public void update(Object JavaDoc object)
272    throws HibernateException
273    {
274       getHibernateSession().update(object);
275    }
276
277    public void update(String JavaDoc entityName, Object JavaDoc object)
278    throws HibernateException
279    {
280       getHibernateSession().update(entityName, object);
281    }
282
283    public Object JavaDoc merge(Object JavaDoc object)
284    throws HibernateException
285    {
286       return getHibernateSession().merge(object);
287    }
288
289    public Object JavaDoc merge(String JavaDoc entityName, Object JavaDoc object)
290    throws HibernateException
291    {
292       return getHibernateSession().merge(entityName, object);
293    }
294
295    public void persist(Object JavaDoc object)
296    throws HibernateException
297    {
298       getHibernateSession().persist(object);
299    }
300
301    public void persist(String JavaDoc entityName, Object JavaDoc object)
302    throws HibernateException
303    {
304       getHibernateSession().persist(entityName, object);
305    }
306
307    public void delete(Object JavaDoc object)
308    throws HibernateException
309    {
310       getHibernateSession().delete(object);
311    }
312
313    public void delete(String JavaDoc entityName, Object JavaDoc object)
314    throws HibernateException
315    {
316       getHibernateSession().delete(entityName, object);
317    }
318
319    public void lock(Object JavaDoc object, LockMode lockMode)
320    throws HibernateException
321    {
322       getHibernateSession().lock(object, lockMode);
323    }
324
325    public void lock(String JavaDoc entityName, Object JavaDoc object, LockMode lockMode)
326    throws HibernateException
327    {
328       getHibernateSession().lock(entityName, object, lockMode);
329    }
330
331    public void refresh(Object JavaDoc object)
332    throws HibernateException
333    {
334       getHibernateSession().refresh(object);
335    }
336
337    public void refresh(Object JavaDoc object, LockMode lockMode)
338    throws HibernateException
339    {
340       getHibernateSession().refresh(object, lockMode);
341    }
342
343    public LockMode getCurrentLockMode(Object JavaDoc object)
344    throws HibernateException
345    {
346       return getHibernateSession().getCurrentLockMode(object);
347    }
348
349    public Transaction beginTransaction()
350    throws HibernateException
351    {
352       return getHibernateSession().beginTransaction();
353    }
354
355    public Criteria createCriteria(Class JavaDoc persistentClass)
356    {
357       return getHibernateSession().createCriteria(persistentClass);
358    }
359
360    public Criteria createCriteria(Class JavaDoc persistentClass, String JavaDoc alias)
361    {
362       return getHibernateSession().createCriteria(persistentClass, alias);
363    }
364
365    public Criteria createCriteria(String JavaDoc entityName)
366    {
367       return getHibernateSession().createCriteria(entityName);
368    }
369
370    public Criteria createCriteria(String JavaDoc entityName, String JavaDoc alias)
371    {
372       return getHibernateSession().createCriteria(entityName, alias);
373    }
374
375    public org.hibernate.Query createQuery(String JavaDoc queryString)
376    throws HibernateException
377    {
378       return getHibernateSession().createQuery(queryString);
379    }
380
381    public SQLQuery createSQLQuery(String JavaDoc queryString)
382    throws HibernateException
383    {
384       return getHibernateSession().createSQLQuery(queryString);
385    }
386
387    public org.hibernate.Query createFilter(Object JavaDoc collection, String JavaDoc queryString)
388    throws HibernateException
389    {
390       return getHibernateSession().createFilter(collection, queryString);
391    }
392
393    public org.hibernate.Query getNamedQuery(String JavaDoc queryName)
394    throws HibernateException
395    {
396       return getHibernateSession().getNamedQuery(queryName);
397    }
398
399    public void clear()
400    {
401       getHibernateSession().clear();
402    }
403
404    public Object JavaDoc get(Class JavaDoc clazz, Serializable JavaDoc id)
405    throws HibernateException
406    {
407       return getHibernateSession().get(clazz, id);
408    }
409
410    public Object JavaDoc get(Class JavaDoc clazz, Serializable JavaDoc id, LockMode lockMode)
411    throws HibernateException
412    {
413       return getHibernateSession().get(clazz, id, lockMode);
414    }
415
416    public Object JavaDoc get(String JavaDoc entityName, Serializable JavaDoc id)
417    throws HibernateException
418    {
419       return getHibernateSession().get(entityName, id);
420    }
421
422    public Object JavaDoc get(String JavaDoc entityName, Serializable JavaDoc id, LockMode lockMode)
423    throws HibernateException
424    {
425       return getHibernateSession().get(entityName, id, lockMode);
426    }
427
428    public String JavaDoc getEntityName(Object JavaDoc object)
429    throws HibernateException
430    {
431       return getHibernateSession().getEntityName(object);
432    }
433
434    public Filter enableFilter(String JavaDoc filterName)
435    {
436       return getHibernateSession().enableFilter(filterName);
437    }
438
439    public Filter getEnabledFilter(String JavaDoc filterName)
440    {
441       return getHibernateSession().getEnabledFilter(filterName);
442    }
443
444    public void disableFilter(String JavaDoc filterName)
445    {
446       getHibernateSession().disableFilter(filterName);
447    }
448
449    public SessionStatistics getStatistics()
450    {
451       return getHibernateSession().getStatistics();
452    }
453
454    public void setReadOnly(Object JavaDoc entity, boolean readOnly)
455    {
456       getHibernateSession().setReadOnly(entity, readOnly);
457    }
458
459    public Transaction getTransaction()
460    {
461       return getHibernateSession().getTransaction();
462    }
463 }
464
Popular Tags