全局hibernate过滤所有数据库查询 [英] Global hibernate filter on all database queries

查看:193
本文介绍了全局hibernate过滤所有数据库查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为我的web应用程序使用Spring MVC和Hibernate。我正在寻找一种方法来创建一个适用于我的DAO类中的每个查询的全局hibernate筛选器,而不必在每个DAO方法中明确启用它。

要求是通过用户选择的会话变量来过滤记录。所以我们的查询参数将被保留在会话中,并且该会话中的所有DAO查询都需要通过此变量过滤结果。这里的目的是为了避免每个DAO方法中的所有可重复的过滤代码。



欢迎任何和所有想法!

解决方案

我在这里处理的方式。以下是基于与@ Rp的讨论 - 以及这里提供的建议

a>。

三个主要元素用于配置:

- Spring的会话范围bean

- package-info.java

- Spring AOP









我创建了一个会话作用域Spring bean ,它将保存我的用户选择变量。该变量将根据用户的请求通过弹簧控制器映射方法进行修改。由于Spring的托管bean,我可以通过Spring的依赖注入访问应用程序中任何位置的session变量。

  @Component 
@Scope(value =session,proxyMode = ScopedProxyMode.TARGET_CLASS)
public class SessionParam实现Serializable {

private String sessParam;
..
..
}






接下来,我在包级别定义我的 hibernate过滤器
这在 package-info.java 文件中完成。


  @FilterDef(name =GLOBAL_FILTER,parameters = {@ParamDef(name =sessParam,type =string)},
defaultCondition =sessParam =:sessParam)

package com.company.app.entity;
import org.hibernate.annotations.FilterDef;
import org.hibernate.annotations.FilterDefs;
import org.hibernate.annotations.ParamDef;



包中的实体使用hibernate的@Filter注释进行注释,如下所示:

  @Entity 
@Filter(name =GLOBAL_FILTER)
@Table(name =TABLE_XYZ ,schema =SCHEMA_ABC)
public class TableXyz implements Serializable {
...
}






最后,在hibernate的Session Factory的getCurrentSession()方法中,使用 AspectJ 方面拦截所有DAO查询。



以下是Aspect类。

  @Aspect 
@Component
public class GlobalFilter {

@Autowired
SessionParam sessionParam;

$ b @Pointcut(execution(* org.hibernate.SessionFactory.getCurrentSession(..)))
protected void hibernateSessionFetch(){



@AfterReturning(pointcut =hibernateSessionFetch(),returns =result)
public void enableGlobalFilter(JoinPoint joinPoint,Object result){

会话会话=(会话)结果;

session.enableFilter(GLOBAL_FILTER)。setParameter(sessParam,sessionParam.getSessParam());


$ b $ / code $ / pre



对具有GLOBAL_FILTER的实体的所有查询现在都对所需变量进行条件检查。在DAO方法中,每个查询都不需要显式的条件检查。


I am using Spring MVC and Hibernate for my web application. I am looking for a way to create a global hibernate filter of sorts that would apply to each query in my DAO classes without me having to explicitly enable it in each DAO method.

The requirement is to filter records by a user selected session variable. So our query parameter would be held in session and all DAO queries in that session need to filter results by this variable. The purpose here is to avoid all the repeatable filtering code in every DAO method.

Any and all ideas are welcome!

解决方案

Putting out the way I handled this down here. Below is based on the discussion with @Rp- and suggestions made here.

Three major elements went into configuring this:
- Spring's session scoped beans
- package-info.java
- Spring AOP



I created a session scoped Spring bean that will hold my user selected variable. The variable would get modified upon user's request through a spring Controller mapping method. Being held in a spring managed bean, I have access to the session variable anywhere in my application by virtue of spring's dependency injection.

@Component
@Scope(value="session", proxyMode=ScopedProxyMode.TARGET_CLASS)
public class SessionParam implements Serializable{

  private String sessParam;
..
..
}


Next I define my hibernate filter at package level. This is done in package-info.java file. All entities in this package thus inherit this filter.

 @FilterDef(name="GLOBAL_FILTER",   parameters = {@ParamDef(name="sessParam", type="string")}, 
                                    defaultCondition = "sessParam = :sessParam")

package com.company.app.entity;
import org.hibernate.annotations.FilterDef;
import org.hibernate.annotations.FilterDefs;
import org.hibernate.annotations.ParamDef;


Entities in the package are annotated with hibernate's @Filter annotation as below:

@Entity
@Filter(name="GLOBAL_FILTER")
@Table(name = "TABLE_XYZ", schema = "SCHEMA_ABC")
public class TableXyz implements Serializable {
...
}


Lastly, all DAO queries are intercepted using an AspectJ aspect on hibernate's Session Factory's getCurrentSession() method.

Below is the Aspect class.

@Aspect
@Component
public class GlobalFilter {

    @Autowired
    SessionParam sessionParam;


    @Pointcut("execution(* org.hibernate.SessionFactory.getCurrentSession(..))")
    protected void hibernateSessionFetch(){

    }

    @AfterReturning(pointcut = "hibernateSessionFetch()", returning = "result")
    public void enableGlobalFilter(JoinPoint joinPoint, Object result){

        Session session = (Session) result;

        session.enableFilter("GLOBAL_FILTER").setParameter("sessParam", sessionParam.getSessParam());

    }
}


All queries on entities with "GLOBAL_FILTER" now have a conditional check on the required variable. No explicit conditional checks in each query is required in the DAO methods.

这篇关于全局hibernate过滤所有数据库查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆