I'm trying to retrieve current entityManager when calling methods from a certain repo. Do you have an idea how I could merge these 2 pointcuts ?
@Pointcut("execution(* com.package.repository.MyRepo.*(..))")
public void insideRepository() {}
@Pointcut("execution(* javax.persistence.EntityManagerFactory+.createEntityManager(..))")
public void createEntityManager() {}
@Before("insideRepository()")
public void enableFilter(JoinPoint jp) {
System.out.println("Inside repo");
}
@AfterReturning(pointcut = "createEntityManager()", returning = "entityManager")
public void enableFilter(EntityManager entityManager) {
try {
entityManager.unwrap(Session.class).enableFilter("myFilter");
} catch (Exception e) {
System.out.println("No filter myFilter for this entity");
}
}
This current solution is working, but I would like to not trigger the catch and call enableFilter only when a method from my repo is called.
Btw, I had to do this tricks because having ...
@PersistenceUnit(unitName = "mySecondDb")
EntityManagerFactory emf;
// OR
@PersistenceContext(unitName = "mySecondDb")
EntityManager em;
... was not working :(