KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.hibernate.Session;
25 import org.hibernate.ejb.HibernateEntityManager;
26 import org.jboss.ejb3.PersistenceUnitRegistry;
27
28 import javax.persistence.*;
29 import java.io.Externalizable JavaDoc;
30 import java.io.IOException JavaDoc;
31 import java.io.ObjectInput JavaDoc;
32 import java.io.ObjectOutput JavaDoc;
33
34
35 /**
36  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
37  * @version $Revision: 42263 $
38  */

39 public class TransactionScopedEntityManager implements EntityManager, HibernateSession, Externalizable JavaDoc
40 {
41    private transient ManagedEntityManagerFactory factory;
42
43    public Session getHibernateSession()
44    {
45       EntityManager em = factory.getTransactionScopedEntityManager();
46       if (em instanceof HibernateEntityManager)
47       {
48          return ((HibernateEntityManager) em).getSession();
49       }
50       throw new RuntimeException JavaDoc("ILLEGAL ACTION: Not a Hibernate pe" +
51               "rsistence provider");
52    }
53
54    public TransactionScopedEntityManager(ManagedEntityManagerFactory factory)
55    {
56       if (factory == null) throw new NullPointerException JavaDoc("factory must not be null");
57       this.factory = factory;
58    }
59
60    public TransactionScopedEntityManager()
61    {
62    }
63
64    public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc
65    {
66       out.writeUTF(factory.getKernelName());
67    }
68
69    public void readExternal(ObjectInput JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc
70    {
71       String JavaDoc kernelName = in.readUTF();
72       PersistenceUnitDeployment deployment = PersistenceUnitRegistry.getPersistenceUnit(kernelName);
73       if (deployment == null) throw new IOException JavaDoc("Unable to find persistence unit in registry: " + kernelName);
74       factory = deployment.getManagedFactory();
75    }
76
77    public Object JavaDoc getDelegate()
78    {
79       return factory.getTransactionScopedEntityManager();
80    }
81
82    public void joinTransaction()
83    {
84       factory.verifyInTx();
85       factory.getTransactionScopedEntityManager().joinTransaction();
86    }
87
88    public void clear()
89    {
90       factory.getTransactionScopedEntityManager().clear();
91    }
92
93    public FlushModeType getFlushMode()
94    {
95       return factory.getTransactionScopedEntityManager().getFlushMode();
96    }
97
98    public void lock(Object JavaDoc entity, LockModeType lockMode)
99    {
100       factory.verifyInTx();
101       factory.getTransactionScopedEntityManager().lock(entity, lockMode);
102    }
103
104    public <T> T getReference(Class JavaDoc<T> entityClass, Object JavaDoc primaryKey)
105    {
106       EntityManager em = factory.getTransactionScopedEntityManager();
107       if (!factory.isInTx()) em.clear(); // em will be closed by interceptor
108
try
109       {
110          return em.getReference(entityClass, primaryKey);
111       }
112       finally
113       {
114          if (!factory.isInTx()) em.clear(); // em will be closed by interceptor
115
}
116    }
117
118    public void setFlushMode(FlushModeType flushMode)
119    {
120       factory.getTransactionScopedEntityManager().setFlushMode(flushMode);
121    }
122
123    public Query createQuery(String JavaDoc ejbqlString)
124    {
125       EntityManager em = factory.getTransactionScopedEntityManager();
126       if (!factory.isInTx()) em.clear(); // em will be closed by interceptor
127
return em.createQuery(ejbqlString);
128    }
129
130    public Query createNamedQuery(String JavaDoc name)
131    {
132       EntityManager em = factory.getTransactionScopedEntityManager();
133       if (!factory.isInTx()) em.clear(); // em will be closed by interceptor
134
return em.createNamedQuery(name);
135    }
136
137    public Query createNativeQuery(String JavaDoc sqlString)
138    {
139       EntityManager em = factory.getTransactionScopedEntityManager();
140       if (!factory.isInTx()) em.clear(); // em will be closed by interceptor
141
return em.createNativeQuery(sqlString);
142    }
143
144    public Query createNativeQuery(String JavaDoc sqlString, Class JavaDoc resultClass)
145    {
146       EntityManager em = factory.getTransactionScopedEntityManager();
147       if (!factory.isInTx()) em.clear(); // em will be closed by interceptor
148
return em.createNativeQuery(sqlString, resultClass);
149    }
150
151    public Query createNativeQuery(String JavaDoc sqlString, String JavaDoc resultSetMapping)
152    {
153       EntityManager em = factory.getTransactionScopedEntityManager();
154       if (!factory.isInTx()) em.clear(); // em will be closed by interceptor
155
return em.createNativeQuery(sqlString, resultSetMapping);
156    }
157
158    public <A> A find(Class JavaDoc<A> entityClass, Object JavaDoc primaryKey)
159    {
160       EntityManager em = factory.getTransactionScopedEntityManager();
161       if (!factory.isInTx()) em.clear(); // em will be closed by interceptor
162
try
163       {
164          return em.find(entityClass, primaryKey);
165       }
166       finally
167       {
168          if (!factory.isInTx()) em.clear(); // em will be closed by interceptor
169
}
170    }
171
172    public void persist(Object JavaDoc entity)
173    {
174       factory.verifyInTx();
175       factory.getTransactionScopedEntityManager().persist(entity);
176    }
177
178    public <A> A merge(A entity)
179    {
180       factory.verifyInTx();
181       return (A) factory.getTransactionScopedEntityManager().merge(entity);
182    }
183
184    public void remove(Object JavaDoc entity)
185    {
186       factory.verifyInTx();
187       factory.getTransactionScopedEntityManager().remove(entity);
188    }
189
190    public void refresh(Object JavaDoc entity)
191    {
192       factory.verifyInTx();
193       factory.getTransactionScopedEntityManager().refresh(entity);
194    }
195
196    public boolean contains(Object JavaDoc entity)
197    {
198       return factory.getTransactionScopedEntityManager().contains(entity);
199    }
200
201    public void flush()
202    {
203       factory.verifyInTx();
204       factory.getTransactionScopedEntityManager().flush();
205    }
206
207    public void close()
208    {
209       throw new IllegalStateException JavaDoc("Illegal to call this method from injected, managed EntityManager");
210    }
211
212    public boolean isOpen()
213    {
214       throw new IllegalStateException JavaDoc("Illegal to call this method from injected, managed EntityManager");
215    }
216
217    public EntityTransaction getTransaction()
218    {
219       throw new IllegalStateException JavaDoc("Illegal to call this method from injected, managed EntityManager");
220    }
221
222 }
223
Popular Tags