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

前端 未结 2 1863
遥遥无期
遥遥无期 2020-12-03 18:32

In a Spring JPA + Hibernate environment I need to enable Hibernate entity filters. So I should have access to Hibernate Session object, but I\'m using EntityManagerFactory a

相关标签:
2条回答
  • 2020-12-03 19:30

    I ended up with AOP solution :

    @Aspect
    @Component
    public 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");
            }
        }
    
    }
    
    0 讨论(0)
  • 2020-12-03 19:37

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

        entityManager.unwrap(Session.class)
                .enableFilter("notDeleted")
                .setParameter("isDeleted", false);
    
    0 讨论(0)
提交回复
热议问题