Skip to content

Commit ab0d760

Browse files
committed
Merge pull request #303 from olivergierke/SPR-10678
# By Oliver Gierke * SPR-10678: Fixed potential NPE in SharedEntityManagerCreator
2 parents b704d62 + b552c6e commit ab0d760

File tree

2 files changed

+52
-4
lines changed

2 files changed

+52
-4
lines changed

spring-orm/src/main/java/org/springframework/orm/jpa/SharedEntityManagerCreator.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@
2424
import java.lang.reflect.Method;
2525
import java.lang.reflect.Proxy;
2626
import java.util.Map;
27+
2728
import javax.persistence.EntityManager;
2829
import javax.persistence.EntityManagerFactory;
2930
import javax.persistence.Query;
3031

3132
import org.apache.commons.logging.Log;
3233
import org.apache.commons.logging.LogFactory;
33-
3434
import org.springframework.util.ClassUtils;
3535
import org.springframework.util.CollectionUtils;
3636

@@ -46,12 +46,16 @@
4646
*
4747
* @author Juergen Hoeller
4848
* @author Rod Johnson
49+
* @author Oliver Gierke
4950
* @since 2.0
5051
* @see org.springframework.orm.jpa.LocalEntityManagerFactoryBean
5152
* @see org.springframework.orm.jpa.JpaTransactionManager
5253
*/
5354
public abstract class SharedEntityManagerCreator {
5455

56+
private static final Class<?>[] NO_ENTITY_MANAGER_INTERFACES = new Class<?>[0];
57+
58+
5559
/**
5660
* Create a transactional EntityManager proxy for the given EntityManagerFactory,
5761
* automatically joining ongoing transactions.
@@ -84,10 +88,11 @@ public static EntityManager createSharedEntityManager(EntityManagerFactory emf,
8488
*/
8589
public static EntityManager createSharedEntityManager(
8690
EntityManagerFactory emf, Map properties, boolean synchronizedWithTransaction) {
87-
88-
Class emIfc = (emf instanceof EntityManagerFactoryInfo ?
91+
Class<?> entityManagerInterface = (emf instanceof EntityManagerFactoryInfo ?
8992
((EntityManagerFactoryInfo) emf).getEntityManagerInterface() : EntityManager.class);
90-
return createSharedEntityManager(emf, properties, synchronizedWithTransaction, emIfc);
93+
return createSharedEntityManager(emf, properties, synchronizedWithTransaction,
94+
(entityManagerInterface == null ? NO_ENTITY_MANAGER_INTERFACES :
95+
new Class<?>[] { entityManagerInterface }));
9196
}
9297

9398
/**
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2002-2013 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.orm.jpa;
18+
19+
import javax.persistence.EntityManagerFactory;
20+
21+
import org.junit.Test;
22+
23+
import static org.hamcrest.CoreMatchers.*;
24+
import static org.junit.Assert.*;
25+
import static org.mockito.Mockito.*;
26+
27+
/**
28+
* Unit tests for {@link SharedEntityManagerCreator}.
29+
*
30+
* @author Oliver Gierke
31+
*/
32+
public class SharedEntityManagerCreatorTests {
33+
34+
@Test
35+
public void proxyingWorksIfInfoReturnsNullEntityManagerInterface() {
36+
EntityManagerFactory emf = mock(EntityManagerFactory.class,
37+
withSettings().extraInterfaces(EntityManagerFactoryInfo.class));
38+
// EntityManagerFactoryInfo.getEntityManagerInterface returns null
39+
assertThat(SharedEntityManagerCreator.createSharedEntityManager(emf),
40+
is(notNullValue()));
41+
}
42+
43+
}

0 commit comments

Comments
 (0)