KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > oracle > toplink > essentials > internal > ejb > cmp3 > EntityManagerImpl


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21 // Copyright (c) 1998, 2006, Oracle. All rights reserved.
22
package oracle.toplink.essentials.internal.ejb.cmp3;
23
24 import java.util.Map JavaDoc;
25
26 import javax.persistence.FlushModeType;
27 import javax.persistence.Query;
28
29 import oracle.toplink.essentials.exceptions.EJBQLException;
30 import oracle.toplink.essentials.expressions.Expression;
31 import oracle.toplink.essentials.queryframework.DatabaseQuery;
32 import oracle.toplink.essentials.queryframework.ResultSetMappingQuery;
33 import oracle.toplink.essentials.threetier.ServerSession;
34 import oracle.toplink.essentials.internal.ejb.cmp3.transaction.*;
35 import oracle.toplink.essentials.internal.localization.ExceptionLocalization;
36
37 /**
38 * <p>
39 * <b>Purpose</b>: Contains the implementation of the EntityManager.
40 * <p>
41 * <b>Description</b>: This class provides the implementation for the combined TopLink
42 * and EJB3.0 EntityManager class.
43 * <p>
44 * <b>Responsibilities</b>:It is responcible for tracking transaction state and the
45 * objects within that transaction.
46 * @see javax.persistence.EntityManager
47 * @see oracle.toplink.essentials.ejb.cmp3.EntityManager
48 */

49
50 /* @author gyorke
51  * @since TopLink 10.1.3 EJB 3.0 Preview
52  */

53
54
55 public class EntityManagerImpl
56     extends oracle.toplink.essentials.internal.ejb.cmp3.base.EntityManagerImpl
57     implements oracle.toplink.essentials.ejb.cmp3.EntityManager
58 {
59    
60     private FlushModeType flushMode;
61     
62     /**
63      * Constructor returns an EntityManager assigned to the a particular ServerSession.
64      * @param sessionName the ServerSession name that should be used.
65      * This constructor can potentially throw TopLink exceptions regarding the existence, or
66      * errors with the specified session.
67      */

68     public EntityManagerImpl(String JavaDoc sessionName, boolean propagatePersistenceContext, boolean extended){
69         super(sessionName, propagatePersistenceContext, extended);
70         flushMode = FlushModeType.AUTO;
71     }
72
73    /**
74      * Constructor called from the EntityManagerFactory to create an EntityManager
75      * @param serverSession the serverSession assigned to this deployment.
76      */

77     public EntityManagerImpl(ServerSession serverSession, boolean propagatePersistenceContext, boolean extended){
78         this(serverSession, null, propagatePersistenceContext, extended);
79     }
80
81     /**
82      * Constructor called from the EntityManagerFactory to create an EntityManager
83      * @param serverSession the serverSession assigned to this deployment.
84      * Note: The properties argument is provided to allow properties to be passed into this EntityManager,
85      * but there are currently no such properties implemented
86      */

87     public EntityManagerImpl(ServerSession serverSession, Map JavaDoc properties, boolean propagePersistenceContext, boolean extended){
88         super(serverSession, properties, propagePersistenceContext, extended);
89         flushMode = FlushModeType.AUTO;
90     }
91     
92     /**
93      * Constructor called from the EntityManagerFactory to create an EntityManager
94      * @param factory the EntityMangerFactoryImpl that created this entity manager.
95      * Note: The properties argument is provided to allow properties to be passed into this EntityManager,
96      * but there are currently no such properties implemented
97      */

98     public EntityManagerImpl(EntityManagerFactoryImpl factory, Map JavaDoc properties, boolean propagePersistenceContext, boolean extended){
99         super(factory, properties, propagePersistenceContext, extended);
100         flushMode = FlushModeType.AUTO;
101     }
102     
103     /**
104      * Merge the state of the given entity into the
105      * current persistence context, using the unqualified
106      * class name as the entity name.
107      * @param entity
108      * @return the instance that the state was merged to
109      */

110     public <T> T merge(T entity){
111         try{
112                 return (T) mergeInternal(entity);
113             }catch (RuntimeException JavaDoc e){
114                 this.transaction.setRollbackOnlyInternal();
115                 throw e;
116             }
117     }
118     
119     /**
120     * Find by primary key.
121     * @param entityClass
122     * @param primaryKey
123     * @return the found entity instance
124     * or null if the entity does not exist
125     * @throws IllegalArgumentException if the first argument does
126     * not denote an entity type or the second argument is not a valid type for that
127     * entity's primary key
128     */

129     public <T> T find(Class JavaDoc<T> entityClass, Object JavaDoc primaryKey){
130         return (T) findInternal(entityClass, primaryKey);
131     }
132     
133     /**
134      * Get an instance, whose state may be lazily fetched.
135      * If the requested instance does not exist in the database,
136      * throws EntityNotFoundException when the instance state is first accessed.
137      * (The container is permitted to throw EntityNotFoundException when get is called.)
138      * The application should not expect that the instance state will
139      * be available upon detachment, unless it was accessed by the
140      * application while the entity manager was open.
141      * @param entityClass
142      * @param primaryKey
143      * @return the found entity instance
144      * @throws IllegalArgumentException if the first argument does
145      * not denote an entity type or the second
146      * argument is not a valid type for that
147      * entity's primary key
148      * @throws EntityNotFoundException if the entity state
149      * cannot be accessed
150      */

151     public <T> T getReference(Class JavaDoc<T> entityClass, Object JavaDoc primaryKey) {
152         Object JavaDoc returnValue = findInternal(entityClass, primaryKey);
153         if (returnValue ==null){
154             Object JavaDoc[] o = {primaryKey};
155             String JavaDoc message = ExceptionLocalization.buildMessage("no_entities_retrieved_for_get_reference", o);
156             throw new javax.persistence.EntityNotFoundException(message);
157         }
158         return (T)returnValue;
159     }
160     
161     /**
162     * Create an instance of Query for executing an
163     * EJBQL query.
164     * @param ejbqlString an EJBQL query string
165     * @return the new query instance
166     */

167     public Query createQuery(String JavaDoc ejbqlString){
168     
169         verifyOpen();
170         
171         EJBQueryImpl ejbqImpl;
172         
173         try
174         {
175             ejbqImpl = new EJBQueryImpl(ejbqlString, this);
176         }
177         
178         catch(EJBQLException ex)
179         {
180             throw new IllegalArgumentException JavaDoc(ExceptionLocalization.buildMessage("wrap_ejbql_exception"), ex);
181         }
182         
183         return ejbqImpl;
184     }
185     /**
186     * Create an instance of Query for executing a
187     * named query (in EJBQL or native SQL).
188     * @param name the name of a query defined in metadata
189     * @return the new query instance
190     */

191     public Query createNamedQuery(String JavaDoc name){
192         verifyOpen();
193         return new EJBQueryImpl(name, this, true);
194     }
195     /**
196     * Create an instance of Query for executing
197     * a native SQL query.
198     * @param sqlString a native SQL query string
199     * @return the new query instance
200     */

201     public Query createNativeQuery(String JavaDoc sqlString){
202         verifyOpen();
203         return new EJBQueryImpl( EJBQueryImpl.buildSQLDatabaseQuery( sqlString, false), this );
204     }
205
206     /**
207      * This method is used to create a query using SQL. The class, must be the expected
208      * return type.
209      */

210     public javax.persistence.Query createNativeQuery(String JavaDoc sqlString, Class JavaDoc resultType){
211         DatabaseQuery query = createNativeQueryInternal(sqlString, resultType);
212         return new EJBQueryImpl(query, this);
213     }
214      
215     /**
216     * Create an instance of Query for executing
217     * a native SQL query.
218     * @param sqlString a native SQL query string
219     * @param resultSetMapping the name of the result set mapping
220     * @return the new query instance
221     * @throws IllegalArgumentException if query string is not valid
222     */

223     public Query createNativeQuery(String JavaDoc sqlString, String JavaDoc resultSetMapping){
224         verifyOpen();
225         ResultSetMappingQuery query = new ResultSetMappingQuery();
226         query.setSQLResultSetMappingName(resultSetMapping);
227         query.setSQLString(sqlString);
228         query.setIsUserDefined(true);
229         return new EJBQueryImpl(query, this);
230     }
231
232     /**
233     * Get the flush mode that applies to all objects contained
234     * in the persistence context.
235     * @return flushMode
236     */

237     public FlushModeType getFlushMode() {
238         return flushMode;
239     }
240
241     /**
242     * Set the flush mode that applies to all objects contained
243     * in the persistence context.
244     * @param flushMode
245     */

246     public void setFlushMode(FlushModeType flushMode) {
247         this.flushMode = flushMode;
248     }
249     
250     /**
251      * This method is used to create a query using a Toplink Expression and the return type.
252      */

253     public javax.persistence.Query createQuery(Expression expression, Class JavaDoc resultType){
254         DatabaseQuery query = createQueryInternal(expression, resultType);
255         return new EJBQueryImpl(query, this);
256     }
257
258     /**
259      * Returns the resource-level transaction object.
260      * The EntityTransaction instance may be used serially to
261      * begin and commit multiple transactions.
262      * @return EntityTransaction instance
263      * @throws IllegalStateException if invoked on a JTA
264      * EntityManager or an EntityManager that has been closed.
265      */

266     public javax.persistence.EntityTransaction getTransaction(){
267         return ((TransactionWrapper)transaction).getTransaction();
268     }
269
270     /**
271     * Internal method. Indicates whether flushMode is AUTO.
272     * @return boolean
273     */

274     public boolean isFlushModeAUTO() {
275         return flushMode == FlushModeType.AUTO;
276     }
277     
278     protected void setJTATransactionWrapper() {
279         transaction = new JTATransactionWrapper(this);
280     }
281     
282     protected void setEntityTransactionWrapper() {
283         transaction = new EntityTransactionWrapper(this);
284     }
285 }
286
Popular Tags