Access to Session using Spring JPA and Hibernate in order to enable filters Access to Session using Spring JPA and Hibernate in order to enable filters spring spring

Access to Session using Spring JPA and Hibernate in order to enable filters


I ended up with AOP solution :

@Aspect@Componentpublic class EnableFilterAspect {    @AfterReturning(            pointcut="bean(entityManagerFactory) && execution(* createEntityManager(..))",            returning="retVal")    public void getSessionAfter(JoinPoint joinPoint, Object retVal) {        if (retVal != null && EntityManager.class.isInstance(retVal)) {            Session session = ((EntityManager) retVal).unwrap(Session.class);            session.enableFilter("myFilter").setParameter("myParameter", "myValue");        }    }}


here's one i use for apps that support is_delete on objects -

    entityManager.unwrap(Session.class)            .enableFilter("notDeleted")            .setParameter("isDeleted", false);