KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > persistence > TxEntityManager


1 /**
2  * EasyBeans
3  * Copyright (C) 2006 Bull S.A.S.
4  * Contact: easybeans@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: TxEntityManager.java 326 2006-04-14 08:20:59Z benoitf $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.easybeans.persistence;
27
28 import javax.persistence.EntityManager;
29 import javax.persistence.EntityNotFoundException;
30 import javax.persistence.EntityTransaction;
31 import javax.persistence.FlushModeType;
32 import javax.persistence.LockModeType;
33 import javax.persistence.PersistenceException;
34 import javax.persistence.Query;
35 import javax.persistence.TransactionRequiredException;
36
37 /**
38  * This class represents an EntityManager that will be used as a container
39  * managed transaction scoped persistence context. The lifetime of this context
40  * is a single transaction. When the transaction is committed or rollbacked, the
41  * persistence context ends.
42  * @author Florent Benoit
43  */

44 public class TxEntityManager implements EntityManager {
45
46     /**
47      * Handler of the manager. Do the switch for each TX.
48      */

49     private TxEntityManagerHandler handler;
50
51     /**
52      * Build a new entity manager which have TransactionScoped type.
53      * @param handler object managing the transaction's EntityManager.
54      */

55     public TxEntityManager(final TxEntityManagerHandler handler) {
56         this.handler = handler;
57     }
58
59     /**
60      * Gets (or create) a new EntityManager for the current tx (if any).
61      * @return an entity manager.
62      */

63     public EntityManager getCurrentEntityManager() {
64         return handler.getCurrent();
65     }
66
67     /**
68      * Make an instance managed and persistent.
69      * @param entity entity bean.
70      * @throws IllegalArgumentException if not an entity or entity is detached
71      * @throws TransactionRequiredException if there is no transaction and the
72      * persistence context is of type PersistenceContextType.TRANSACTION
73      */

74     public void persist(final Object JavaDoc entity) throws IllegalArgumentException JavaDoc, TransactionRequiredException {
75         getCurrentEntityManager().persist(entity);
76     }
77
78     /**
79      * Merge the state of the given entity into the current persistence context.
80      * @param entity entity bean
81      * @param <T> entity object's class.
82      * @return the instance that the state was merged to
83      * @throws IllegalArgumentException if instance is not an entity or is a
84      * removed entity
85      * @throws TransactionRequiredException if there is no transaction and the
86      * persistence context is of type PersistenceContextType.TRANSACTION
87      */

88     public <T> T merge(final T entity) throws IllegalArgumentException JavaDoc, TransactionRequiredException {
89         return getCurrentEntityManager().merge(entity);
90     }
91
92     /**
93      * Remove the entity instance.
94      * @param entity entity bean
95      * @throws IllegalArgumentException if not an entity or if a detached entity
96      * @throws TransactionRequiredException if there is no transaction and the
97      * persistence context is of type PersistenceContextType.TRANSACTION
98      */

99     public void remove(final Object JavaDoc entity) throws IllegalArgumentException JavaDoc, TransactionRequiredException {
100         getCurrentEntityManager().remove(entity);
101     }
102
103     /**
104      * Find by primary key.
105      * @param <T> entity object's class.
106      * @param entityClass the class of the entity
107      * @param primaryKey the primary key
108      * @return the found entity instance or null if the entity does not exist
109      * @throws IllegalArgumentException if the first argument does not denote an
110      * entity type or the second argument is not a valid type for that
111      * entity?s primary key
112      */

113     public <T> T find(final Class JavaDoc<T> entityClass, final Object JavaDoc primaryKey) throws IllegalArgumentException JavaDoc {
114         return getCurrentEntityManager().find(entityClass, primaryKey);
115     }
116
117     /**
118      * Get an instance, whose state may be lazily fetched. If the requested
119      * instance does not exist in the database, throws EntityNotFoundException
120      * when the instance state is first accessed. (The persistence provider
121      * runtime is permitted to throw the EntityNotFoundException when
122      * getReference is called.) The application should not expect that the
123      * instance state will be available upon detachment, unless it was accessed
124      * by the application while the entity manager was open.
125      * @param <T> entity object's class.
126      * @param entityClass the class of the entity
127      * @param primaryKey the primary key
128      * @return the found entity instance
129      * @throws IllegalArgumentException if the first argument does not denote an
130      * entity type or the second argument is not a valid type for that
131      * entity?s primary key
132      * @throws EntityNotFoundException if the entity state cannot be accessed
133      */

134     public <T> T getReference(final Class JavaDoc<T> entityClass, final Object JavaDoc primaryKey) throws IllegalArgumentException JavaDoc,
135             EntityNotFoundException {
136         return getCurrentEntityManager().getReference(entityClass, primaryKey);
137     }
138
139     /**
140      * Synchronize the persistence context to the underlying database.
141      * @throws TransactionRequiredException if there is no transaction
142      * @throws PersistenceException if the flush fails
143      */

144     public void flush() throws TransactionRequiredException, PersistenceException {
145         getCurrentEntityManager().flush();
146     }
147
148     /**
149      * Set the flush mode that applies to all objects contained in the
150      * persistence context.
151      * @param flushMode the mode of flushing
152      */

153     public void setFlushMode(final FlushModeType flushMode) {
154         getCurrentEntityManager().setFlushMode(flushMode);
155
156     }
157
158     /**
159      * Get the flush mode that applies to all objects contained in the
160      * persistence context.
161      * @return flushMode
162      */

163     public FlushModeType getFlushMode() {
164         return getCurrentEntityManager().getFlushMode();
165     }
166
167     /**
168      * Set the lock mode for an entity object contained in the persistence
169      * context.
170      * @param entity entity bean
171      * @param lockMode mode for locking
172      * @throws PersistenceException if an unsupported lock call is made
173      * @throws IllegalArgumentException if the instance is not an entity or is a
174      * detached entity
175      * @throws TransactionRequiredException if there is no transaction
176      */

177     public void lock(final Object JavaDoc entity, final LockModeType lockMode) throws PersistenceException,
178             IllegalArgumentException JavaDoc, TransactionRequiredException {
179         getCurrentEntityManager().lock(entity, lockMode);
180     }
181
182     /**
183      * Refresh the state of the instance from the database, overwriting changes
184      * made to the entity, if any.
185      * @param entity entity bean
186      * @throws IllegalArgumentException if not an entity or entity is not
187      * managed
188      * @throws TransactionRequiredException if there is no transaction and the
189      * persistence context is of type PersistenceContextType.TRANSACTION
190      * @throws EntityNotFoundException if the entity no longer exists in the
191      * database
192      */

193     public void refresh(final Object JavaDoc entity) throws IllegalArgumentException JavaDoc, TransactionRequiredException,
194             EntityNotFoundException {
195         getCurrentEntityManager().refresh(entity);
196     }
197
198     /**
199      * Clear the persistence context, causing all managed entities to become
200      * detached. Changes made to entities that have not been flushed to the
201      * database will not be persisted.
202      */

203     public void clear() {
204         getCurrentEntityManager().clear();
205     }
206
207     /**
208      * Check if the instance belongs to the current persistence context.
209      * @param entity the entity bean
210      * @return true/false
211      * @throws IllegalArgumentException if not an entity
212      */

213     public boolean contains(final Object JavaDoc entity) throws IllegalArgumentException JavaDoc {
214         return getCurrentEntityManager().contains(entity);
215     }
216
217     /**
218      * Create an instance of Query for executing an EJB QL statement.
219      * @param ejbqlString an EJB QL query string
220      * @return the new query instance
221      * @throws IllegalArgumentException if query string is not valid
222      */

223     public Query createQuery(final String JavaDoc ejbqlString) throws IllegalArgumentException JavaDoc {
224         return getCurrentEntityManager().createQuery(ejbqlString);
225     }
226
227     /**
228      * Create an instance of Query for executing a named query (in EJB QL or
229      * native SQL).
230      * @param name the name of a query defined in metadata
231      * @return the new query instance
232      * @throws IllegalArgumentException if a query has not been defined with the
233      * given name
234      */

235     public Query createNamedQuery(final String JavaDoc name) throws IllegalArgumentException JavaDoc {
236         return getCurrentEntityManager().createNamedQuery(name);
237     }
238
239     /**
240      * Create an instance of Query for executing a native SQL statement, e.g.,
241      * for update or delete.
242      * @param sqlString a native SQL query string
243      * @return the new query instance
244      */

245     public Query createNativeQuery(final String JavaDoc sqlString) {
246         return getCurrentEntityManager().createNativeQuery(sqlString);
247     }
248
249     /**
250      * Create an instance of Query for executing a native SQL query.
251      * @param sqlString a native SQL query string
252      * @param resultClass the class of the resulting instance(s)
253      * @return the new query instance
254      */

255     public Query createNativeQuery(final String JavaDoc sqlString, final Class JavaDoc resultClass) {
256         return getCurrentEntityManager().createNativeQuery(sqlString, resultClass);
257     }
258
259     /**
260      * Create an instance of Query for executing a native SQL query.
261      * @param sqlString a native SQL query string
262      * @param resultSetMapping the name of the result set mapping
263      * @return the new query instance
264      */

265     public Query createNativeQuery(final String JavaDoc sqlString, final String JavaDoc resultSetMapping) {
266         return getCurrentEntityManager().createNativeQuery(sqlString, resultSetMapping);
267     }
268
269     /**
270      * TODO: document this.
271      */

272     public void joinTransaction() {
273         getCurrentEntityManager().joinTransaction();
274     }
275
276     /**
277      * @return TODO: document this.
278      */

279     public Object JavaDoc getDelegate() {
280         return getCurrentEntityManager().getDelegate();
281     }
282
283     /**
284      * Close an application-managed EntityManager. After an EntityManager has
285      * been closed, all methods on the EntityManager instance will throw the
286      * IllegalStateException except for isOpen, which will return false. This
287      * method can only be called when the EntityManager is not associated with
288      * an active transaction.
289      * @throws IllegalStateException if the EntityManager is associated with an
290      * active transaction or if the EntityManager is container-managed.
291      */

292     public void close() throws IllegalStateException JavaDoc {
293         getCurrentEntityManager().close();
294     }
295
296     /**
297      * Determine whether the EntityManager is open.
298      * @return true until the EntityManager has been closed.
299      */

300     public boolean isOpen() {
301         return getCurrentEntityManager().isOpen();
302     }
303
304     /**
305      * Return the resource-level transaction object. The EntityTransaction
306      * instance may be used serially to begin and commit multiple transactions.
307      * @return EntityTransaction instance
308      * @throws IllegalStateException if invoked on a JTA EntityManager or an
309      * EntityManager that has been closed.
310      */

311     public EntityTransaction getTransaction() throws IllegalStateException JavaDoc {
312         return getCurrentEntityManager().getTransaction();
313     }
314
315 }
316
Popular Tags