Hi everyone,
I hope you can help me with this one:
I'm writing JUnit tests for a JPA 2.0 application. As you may know, when writing transactional tests, you need to manually flush the entitymanager after every service call (which are annotated with @Transactional as well) (otherwise changes in the unit test are propagated to the entitymanager due to the open persistence context, causing all kind of mayhem when doing multiple calls and changing entities in the same test). Since I do not want to inject the entitymanager in my tests (my tests shouldn't have to know I'm using JPA, they're testing services), I've written an @AspectJ aspect that flushes the EM after every transactional service cal, as all services extends from an abstract base class which has the @Transactional annotation.
It looks like this:
So my test XML config looks like this (services are in an @Configuration class):Code:@Aspect public class DetachEMAfterTransactionalCallAspect { ... @Around("@target(org.springframework.transaction.annotation.Transactional)") public Object doDetach(ProceedingJoinPoint methodInvocation) { ... getEntityManager().flush(); ... } ... }
Unfortunately, my aspect is never called. It looks like the TX aspect changes the objects in such a way that the services no longer contain the @Transactional annotation and because of this, the detach aspect is never called... I though the proxy-target-class made the tx proxy retain all information of the originating class?Code:<beans ...> <context:annotation-driven/> <context:spring-configured/> <tx:annotation-driven proxy-target-class="true"/> <bean id="servicesConfig" class="org.example.services.ServicesConfig"/> <bean id="detachAspect" class="org.example.unittesting.DetachEMAfterTransactionalCallAspect "/> <aop:aspectj-autoproxy/> </beans>
Any ideas?


Reply With Quote