Browse Source

overridden @PersistenceContext annotations on subclass methods are being processed correctly (SPR-8594)

pull/7/head
Juergen Hoeller 14 years ago
parent
commit
49e61d2680
  1. 3
      org.springframework.orm/src/main/java/org/springframework/orm/jpa/support/PersistenceAnnotationBeanPostProcessor.java
  2. 46
      org.springframework.orm/src/test/java/org/springframework/orm/jpa/support/PersistenceInjectionTests.java

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

@ -27,7 +27,6 @@ import java.util.LinkedList;
import java.util.Map; import java.util.Map;
import java.util.Properties; import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory; import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceContext; import javax.persistence.PersistenceContext;
@ -384,7 +383,7 @@ public class PersistenceAnnotationBeanPostProcessor
for (Method method : targetClass.getDeclaredMethods()) { for (Method method : targetClass.getDeclaredMethods()) {
PersistenceContext pc = method.getAnnotation(PersistenceContext.class); PersistenceContext pc = method.getAnnotation(PersistenceContext.class);
PersistenceUnit pu = method.getAnnotation(PersistenceUnit.class); PersistenceUnit pu = method.getAnnotation(PersistenceUnit.class);
if (pc != null || pu != null && if ((pc != null || pu != null) &&
method.equals(ClassUtils.getMostSpecificMethod(method, clazz))) { method.equals(ClassUtils.getMostSpecificMethod(method, clazz))) {
if (Modifier.isStatic(method.getModifiers())) { if (Modifier.isStatic(method.getModifiers())) {
throw new IllegalStateException("Persistence annotations are not supported on static methods"); throw new IllegalStateException("Persistence annotations are not supported on static methods");

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

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2010 the original author or authors. * Copyright 2002-2011 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -93,7 +93,7 @@ public class PersistenceInjectionTests extends AbstractEntityManagerFactoryBeanT
} }
public void testPublicExtendedPersistenceContextSetter() throws Exception { public void testPublicExtendedPersistenceContextSetter() throws Exception {
Object mockEm = (EntityManager) MockControl.createControl(EntityManager.class).getMock(); EntityManager mockEm = MockControl.createControl(EntityManager.class).getMock();
mockEmf.createEntityManager(); mockEmf.createEntityManager();
emfMc.setReturnValue(mockEm, 1); emfMc.setReturnValue(mockEm, 1);
emfMc.replay(); emfMc.replay();
@ -112,6 +112,39 @@ public class PersistenceInjectionTests extends AbstractEntityManagerFactoryBeanT
emfMc.verify(); emfMc.verify();
} }
public void testPublicSpecificExtendedPersistenceContextSetter() throws Exception {
emfMc.replay();
MockControl<EntityManagerFactory> emfMc2 = MockControl.createControl(EntityManagerFactory.class);
EntityManagerFactory mockEmf2 = emfMc2.getMock();
MockControl<EntityManager> emMc2 = MockControl.createControl(EntityManager.class);
EntityManager mockEm2 = emMc2.getMock();
mockEm2.getTransaction();
emMc2.setReturnValue(null, 1);
mockEm2.flush();
emMc2.setVoidCallable(1);
emMc2.replay();
mockEmf2.createEntityManager();
emfMc2.setReturnValue(mockEm2, 1);
emfMc2.replay();
GenericApplicationContext gac = new GenericApplicationContext();
gac.getDefaultListableBeanFactory().registerSingleton("entityManagerFactory", mockEmf);
gac.getDefaultListableBeanFactory().registerSingleton("unit2", mockEmf2);
gac.registerBeanDefinition("annotationProcessor",
new RootBeanDefinition(PersistenceAnnotationBeanPostProcessor.class));
gac.registerBeanDefinition(SpecificPublicPersistenceContextSetter.class.getName(),
new RootBeanDefinition(SpecificPublicPersistenceContextSetter.class));
gac.refresh();
SpecificPublicPersistenceContextSetter bean = (SpecificPublicPersistenceContextSetter) gac.getBean(
SpecificPublicPersistenceContextSetter.class.getName());
assertNotNull(bean.getEntityManager());
bean.getEntityManager().flush();
emfMc.verify();
emfMc2.verify();
}
public void testPublicExtendedPersistenceContextSetterWithSerialization() throws Exception { public void testPublicExtendedPersistenceContextSetterWithSerialization() throws Exception {
DummyInvocationHandler ih = new DummyInvocationHandler(); DummyInvocationHandler ih = new DummyInvocationHandler();
Object mockEm = Proxy.newProxyInstance(getClass().getClassLoader(), new Class[] {EntityManager.class}, ih); Object mockEm = Proxy.newProxyInstance(getClass().getClassLoader(), new Class[] {EntityManager.class}, ih);
@ -747,6 +780,15 @@ public class PersistenceInjectionTests extends AbstractEntityManagerFactoryBeanT
} }
public static class SpecificPublicPersistenceContextSetter extends DefaultPublicPersistenceContextSetter {
@PersistenceContext(unitName="unit2", type = PersistenceContextType.EXTENDED)
public void setEntityManager(EntityManager em) {
super.setEntityManager(em);
}
}
public static class DefaultPrivatePersistenceUnitField { public static class DefaultPrivatePersistenceUnitField {
@PersistenceUnit @PersistenceUnit

Loading…
Cancel
Save