如何在Hibernate拦截器中获得Hibernate会话? [英] How to get Hibernate session inside a Hibernate Interceptor?

查看:217
本文介绍了如何在Hibernate拦截器中获得Hibernate会话?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Hibernate拦截器中获取Hibernate会话?



我试图使用Hibernate以透明方式强制组织ID访问数据。
我已经设置了一个全局过滤器来按组织ID过滤所有查询。
现在,我需要使用实体拦截器在保存/更新之前在所有实体上设置组织标识。



组织标识来自HttpSession



我在Hibernate会话中将组织标识设置为Filter属性,我想在拦截器内检索它,并将其用于所有插入和更新。
问题是我似乎无法访问拦截器内的Session。任何解决方法?

解决方案

您可以,但我会使用一个简单的POJO来保持事情干净地分开。请记住,存储在单例中的值只能由处理servlet请求的同一个线程访问,所以如果您正在做任何异步操作,则需要对此进行说明。这里有一个超级基本的impl:

  public class OrgId {
public static ThreadLocal< Integer> orgId = new ThreadLocal< Integer>();
}

由于组织ID存在于会话中,因此您可以将在这种早期的servlet过滤器中的ThreadLocal(没有太多错误检查):

  public class OrgIdFilter implements Filter {
public void doFilter(ServletRequest servletrequest,ServletResponse servletresponse,FilterChain filterchain)抛出java.io.IOException,javax.servlet.ServletException {
int orgId = 0;
HttpServletRequest req =(HttpServletRequest)servletRequest;
HttpSession session = req.getSession();
orgId = Integer.parseInt(session.getAttribute(OrganizationalIdAttr));
尝试{
OrgId.orgId.set(orgId);
filterChain.doFilter(servletRequest,servletresponse);
} finally {
OrgId.orgId.set(null); //要求清除后重要!





这个假设orgId是在会话中,当过滤器被调用时,但如果没有,你会明白....

然后在你的拦截器(或者几乎任何地方)你可以得到线程的当前orgId与:

  OrgId.orgId.get(); //可能为空..... 

这里的一个潜在问题是所有这些组件(过滤器,OrgId和拦截器)需要被同一个类加载器加载,以确保OrgId类是一个单例,否则,围绕它的多个ThreadLocal实例将无法一直工作,或根本无法工作。不用说,所有这些都需要在同一个虚拟机中进行。



我不确定这是否是解决此问题最简单的方法,但它确实能帮助您你的组织在哪里你需要它。


How to get Hibernate session inside a Hibernate Interceptor?

I'm trying to use Hibernate to enforce data access by organization id transparently. I have set a global Filter to filter all queries by organization id. Now, I need to use an Entity interceptor to set Organizational Id on all Entities before Save/Update.

The organization id comes from HttpSession

I've set Organizational Id as a Filter property in Hibernate session which i want to retrieve inside my interceptor and use for all Inserts and Updates as well. The problem is i dont seem to have access to Session inside the Interceptor. Any workarounds for this?

解决方案

You can, but I would use a simple POJO just to keep things cleanly separated. Keep in mind that the value stored in the singleton will only be accessible by the same thread that handled the servlet request, so if you're doing any asynch, you will need to account for that. Here's a super basic impl:

public class OrgId {
   public static ThreadLocal<Integer> orgId = new ThreadLocal<Integer>();
}

Since the Organizational Id is resident in the session, you could set the value of the ThreadLocal in an early servlet filter like this (not much error checking):

public class OrgIdFilter implements Filter {
   public void doFilter(ServletRequest servletrequest, ServletResponse servletresponse, FilterChain filterchain) throws java.io.IOException, javax.servlet.ServletException {
      int orgId = 0;
      HttpServletRequest req = (HttpServletRequest) servletRequest;
      HttpSession session = req.getSession();
      orgId = Integer.parseInt(session.getAttribute("OrganizationalIdAttr"));
      try {
         OrgId.orgId.set(orgId);
         filterChain.doFilter(servletRequest, servletresponse);
      } finally {
         OrgId.orgId.set(null); // Important to clear after request !!
      }
   }
}

This assumes that the orgId is in the session when the filter is called, but if not, you get the idea....

Then in your interceptor (or pretty much anywhere) you can get the thread's current orgId with:

OrgId.orgId.get();   // Might be null.....

A potential snafu here is that all these components (filter, OrgId and interceptor) need to be loaded by the same class loader to ensure that the OrgId class is effectively a singleton, otherwise, with multiple instances of the ThreadLocal hanging around it won't work consistently, or at all. Needless to say, all this needs to be happening in the same VM.

I am not sure if this is the cleanest way to solve this problem, but it does get you your orgId where you need it.

这篇关于如何在Hibernate拦截器中获得Hibernate会话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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