Solved it this way: I created an AOP interceptor to catch the EntityManager returned by JpaPersistService.
bindInterceptor(Matchers.subclassesOf(PersistService.class), Matchers.returns(Matchers.identicalTo(EntityManager.class)), new EntityManagerInterceptor() );
Inside the interceptor, I get SessionImpl via EntityManagerImpl and set the autoClear property.
public Object invoke(MethodInvocation invocation) throws Throwable { Object result = invocation.proceed(); if (result instanceof EntityManagerImpl) { EntityManagerImpl castedEm = (EntityManagerImpl) result; ((SessionImpl) castedEm.getSession()).setAutoClear(true); } return result; }
source share