本文介绍了使用Spring JPA和Hibernate访问Session以便启用过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Spring JPA + Hibernate环境中,我需要启用Hibernate实体过滤器.因此,我应该可以访问Hibernate Session对象,但是我正在使用EntityManagerFactory和Spring JPA魔术.有任何会话拦截器,因此每次Spring创建新的会话时,我都可以在其上调用enableFilters()方法吗?

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 and Spring JPA magics.There is any Session interceptor so I can call the enableFilters() method on it every time Spring create a new Session?

推荐答案

我最终获得了AOP解决方案:

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");
        }
    }

}

这篇关于使用Spring JPA和Hibernate访问Session以便启用过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 16:52
查看更多