1
1
/*
2
- * Copyright 2002-2012 the original author or authors.
2
+ * Copyright 2002-2013 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
@@ -76,12 +76,14 @@ public abstract class EntityManagerFactoryUtils {
76
76
* Find an EntityManagerFactory with the given name in the given
77
77
* Spring application context (represented as ListableBeanFactory).
78
78
* <p>The specified unit name will be matched against the configured
79
- * peristence unit, provided that a discovered EntityManagerFactory
79
+ * persistence unit, provided that a discovered EntityManagerFactory
80
80
* implements the {@link EntityManagerFactoryInfo} interface. If not,
81
81
* the persistence unit name will be matched against the Spring bean name,
82
82
* assuming that the EntityManagerFactory bean names follow that convention.
83
+ * <p>If no unit name has been given, this method will search for a default
84
+ * EntityManagerFactory through {@link ListableBeanFactory#getBean(Class)}.
83
85
* @param beanFactory the ListableBeanFactory to search
84
- * @param unitName the name of the persistence unit (may be < code> null</code> or empty,
86
+ * @param unitName the name of the persistence unit (may be {@ code null} or empty,
85
87
* in which case a single bean of type EntityManagerFactory will be searched for)
86
88
* @return the EntityManagerFactory
87
89
* @throws NoSuchBeanDefinitionException if there is no such EntityManagerFactory in the context
@@ -108,17 +110,18 @@ public static EntityManagerFactory findEntityManagerFactory(
108
110
return beanFactory .getBean (unitName , EntityManagerFactory .class );
109
111
}
110
112
else {
111
- return BeanFactoryUtils .beanOfType (beanFactory , EntityManagerFactory .class );
113
+ // Find unique EntityManagerFactory bean in the context, falling back to parent contexts.
114
+ return beanFactory .getBean (EntityManagerFactory .class );
112
115
}
113
116
}
114
117
115
118
/**
116
119
* Obtain a JPA EntityManager from the given factory. Is aware of a
117
120
* corresponding EntityManager bound to the current thread,
118
121
* for example when using JpaTransactionManager.
119
- * <p>Note: Will return < code> null</code> if no thread-bound EntityManager found!
122
+ * <p>Note: Will return {@ code null} if no thread-bound EntityManager found!
120
123
* @param emf EntityManagerFactory to create the EntityManager with
121
- * @return the EntityManager, or < code> null</code> if none found
124
+ * @return the EntityManager, or {@ code null} if none found
122
125
* @throws DataAccessResourceFailureException if the EntityManager couldn't be obtained
123
126
* @see JpaTransactionManager
124
127
*/
@@ -132,11 +135,11 @@ public static EntityManager getTransactionalEntityManager(EntityManagerFactory e
132
135
* Obtain a JPA EntityManager from the given factory. Is aware of a
133
136
* corresponding EntityManager bound to the current thread,
134
137
* for example when using JpaTransactionManager.
135
- * <p>Note: Will return < code> null</code> if no thread-bound EntityManager found!
138
+ * <p>Note: Will return {@ code null} if no thread-bound EntityManager found!
136
139
* @param emf EntityManagerFactory to create the EntityManager with
137
- * @param properties the properties to be passed into the < code> createEntityManager</code>
138
- * call (may be < code> null</code> )
139
- * @return the EntityManager, or < code> null</code> if none found
140
+ * @param properties the properties to be passed into the {@ code createEntityManager}
141
+ * call (may be {@ code null} )
142
+ * @return the EntityManager, or {@ code null} if none found
140
143
* @throws DataAccessResourceFailureException if the EntityManager couldn't be obtained
141
144
* @see JpaTransactionManager
142
145
*/
@@ -154,11 +157,11 @@ public static EntityManager getTransactionalEntityManager(EntityManagerFactory e
154
157
* Obtain a JPA EntityManager from the given factory. Is aware of a
155
158
* corresponding EntityManager bound to the current thread,
156
159
* for example when using JpaTransactionManager.
157
- * <p>Same as < code> getEntityManager</code> , but throwing the original PersistenceException.
160
+ * <p>Same as {@ code getEntityManager} , but throwing the original PersistenceException.
158
161
* @param emf EntityManagerFactory to create the EntityManager with
159
- * @param properties the properties to be passed into the < code> createEntityManager</code>
160
- * call (may be < code> null</code> )
161
- * @return the EntityManager, or < code> null</code> if none found
162
+ * @param properties the properties to be passed into the {@ code createEntityManager}
163
+ * call (may be {@ code null} )
164
+ * @return the EntityManager, or {@ code null} if none found
162
165
* @throws javax.persistence.PersistenceException if the EntityManager couldn't be created
163
166
* @see #getTransactionalEntityManager(javax.persistence.EntityManagerFactory)
164
167
* @see JpaTransactionManager
@@ -273,15 +276,15 @@ public static void applyTransactionTimeout(Query query, EntityManagerFactory emf
273
276
274
277
/**
275
278
* Convert the given runtime exception to an appropriate exception from the
276
- * < code> org.springframework.dao</code> hierarchy.
279
+ * {@ code org.springframework.dao} hierarchy.
277
280
* Return null if no translation is appropriate: any other exception may
278
281
* have resulted from user code, and should not be translated.
279
282
* <p>The most important cases like object not found or optimistic locking failure
280
283
* are covered here. For more fine-granular conversion, JpaTransactionManager etc
281
284
* support sophisticated translation of exceptions via a JpaDialect.
282
285
* @param ex runtime exception that occurred
283
286
* @return the corresponding DataAccessException instance,
284
- * or < code> null</code> if the exception should not be translated
287
+ * or {@ code null} if the exception should not be translated
285
288
*/
286
289
public static DataAccessException convertJpaAccessExceptionIfPossible (RuntimeException ex ) {
287
290
// Following the JPA specification, a persistence provider can also
@@ -317,17 +320,17 @@ public static DataAccessException convertJpaAccessExceptionIfPossible(RuntimeExc
317
320
if (ex instanceof PersistenceException ) {
318
321
return new JpaSystemException ((PersistenceException ) ex );
319
322
}
320
-
323
+
321
324
// If we get here, we have an exception that resulted from user code,
322
325
// rather than the persistence provider, so we return null to indicate
323
326
// that translation should not occur.
324
- return null ;
327
+ return null ;
325
328
}
326
329
327
330
/**
328
331
* Close the given JPA EntityManager,
329
332
* catching and logging any cleanup exceptions thrown.
330
- * @param em the JPA EntityManager to close (may be < code> null</code> )
333
+ * @param em the JPA EntityManager to close (may be {@ code null} )
331
334
* @see javax.persistence.EntityManager#close()
332
335
*/
333
336
public static void closeEntityManager (EntityManager em ) {
0 commit comments