Skip to content

Commit 65077d2

Browse files
committed
overridden @PersistenceContext annotations on subclass methods are being processed correctly (SPR-8594)
1 parent da36c2b commit 65077d2

File tree

2 files changed

+46
-4
lines changed

2 files changed

+46
-4
lines changed

org.springframework.orm/src/main/java/org/springframework/orm/jpa/support/PersistenceAnnotationBeanPostProcessor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2010 the original author or authors.
2+
* Copyright 2002-2011 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -382,7 +382,7 @@ private InjectionMetadata findPersistenceMetadata(final Class clazz) {
382382
for (Method method : targetClass.getDeclaredMethods()) {
383383
PersistenceContext pc = method.getAnnotation(PersistenceContext.class);
384384
PersistenceUnit pu = method.getAnnotation(PersistenceUnit.class);
385-
if (pc != null || pu != null &&
385+
if ((pc != null || pu != null) &&
386386
method.equals(ClassUtils.getMostSpecificMethod(method, clazz))) {
387387
if (Modifier.isStatic(method.getModifiers())) {
388388
throw new IllegalStateException("Persistence annotations are not supported on static methods");

org.springframework.orm/src/test/java/org/springframework/orm/jpa/support/PersistenceInjectionTests.java

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2010 the original author or authors.
2+
* Copyright 2002-2011 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -93,7 +93,7 @@ public void testPrivateVendorSpecificPersistenceContextField() {
9393
}
9494

9595
public void testPublicExtendedPersistenceContextSetter() throws Exception {
96-
Object mockEm = (EntityManager) MockControl.createControl(EntityManager.class).getMock();
96+
EntityManager mockEm = MockControl.createControl(EntityManager.class).getMock();
9797
mockEmf.createEntityManager();
9898
emfMc.setReturnValue(mockEm, 1);
9999
emfMc.replay();
@@ -112,6 +112,39 @@ public void testPublicExtendedPersistenceContextSetter() throws Exception {
112112
emfMc.verify();
113113
}
114114

115+
public void testPublicSpecificExtendedPersistenceContextSetter() throws Exception {
116+
emfMc.replay();
117+
118+
MockControl<EntityManagerFactory> emfMc2 = MockControl.createControl(EntityManagerFactory.class);
119+
EntityManagerFactory mockEmf2 = emfMc2.getMock();
120+
MockControl<EntityManager> emMc2 = MockControl.createControl(EntityManager.class);
121+
EntityManager mockEm2 = emMc2.getMock();
122+
mockEm2.getTransaction();
123+
emMc2.setReturnValue(null, 1);
124+
mockEm2.flush();
125+
emMc2.setVoidCallable(1);
126+
emMc2.replay();
127+
mockEmf2.createEntityManager();
128+
emfMc2.setReturnValue(mockEm2, 1);
129+
emfMc2.replay();
130+
131+
GenericApplicationContext gac = new GenericApplicationContext();
132+
gac.getDefaultListableBeanFactory().registerSingleton("entityManagerFactory", mockEmf);
133+
gac.getDefaultListableBeanFactory().registerSingleton("unit2", mockEmf2);
134+
gac.registerBeanDefinition("annotationProcessor",
135+
new RootBeanDefinition(PersistenceAnnotationBeanPostProcessor.class));
136+
gac.registerBeanDefinition(SpecificPublicPersistenceContextSetter.class.getName(),
137+
new RootBeanDefinition(SpecificPublicPersistenceContextSetter.class));
138+
gac.refresh();
139+
140+
SpecificPublicPersistenceContextSetter bean = (SpecificPublicPersistenceContextSetter) gac.getBean(
141+
SpecificPublicPersistenceContextSetter.class.getName());
142+
assertNotNull(bean.getEntityManager());
143+
bean.getEntityManager().flush();
144+
emfMc.verify();
145+
emfMc2.verify();
146+
}
147+
115148
public void testPublicExtendedPersistenceContextSetterWithSerialization() throws Exception {
116149
DummyInvocationHandler ih = new DummyInvocationHandler();
117150
Object mockEm = Proxy.newProxyInstance(getClass().getClassLoader(), new Class[] {EntityManager.class}, ih);
@@ -747,6 +780,15 @@ public EntityManager getEntityManager() {
747780
}
748781

749782

783+
public static class SpecificPublicPersistenceContextSetter extends DefaultPublicPersistenceContextSetter {
784+
785+
@PersistenceContext(unitName="unit2", type = PersistenceContextType.EXTENDED)
786+
public void setEntityManager(EntityManager em) {
787+
super.setEntityManager(em);
788+
}
789+
}
790+
791+
750792
public static class DefaultPrivatePersistenceUnitField {
751793

752794
@PersistenceUnit

0 commit comments

Comments
 (0)