1

我正在尝试编写一个用于审计目的的 Hibernate 拦截器,但它可以与线程本地上下文会话一起使用(而不是我每次都调用 openSession() 并将其传递给一个拦截器对象)。

任何有关如何执行此操作的指南/示例代码将不胜感激。(我的主要问题是想办法在第一次打开上下文会话时将拦截器对象提供给它)。

4

2 回答 2

2

为什么不使用hibernate的审计solotion?http://docs.jboss.org/envers/docs/index.html

于 2012-08-29T12:12:19.167 回答
2

如果您仅使用 Hibernate,则可以使用两种方法为会话设置拦截器。

//interceptor for global, set interceptor when create sessionFactory with Configure
sessionFactory =
  new AnnotationConfiguration().configure()
    .setInterceptor(new AuditTrailInterceptor())
    .buildSessionFactory()

//interceptor for per Session
Session session = sessionFactory.openSession(new XxxInterceptor())

如果你使用 Spring 创建 SessionFactory

<bean id="sessionFactory"
      class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
  <property name="entityInterceptor">
    <bean class="your.XxxInterceptor"/>
  </property>
  <!-- other  configuration -->
</bean>

我找到了一篇对您有帮助的博文。http://www.sleberknight.com/blog/sleberkn/entry/using_a_hibernate_interceptor_to

于 2012-08-29T13:42:27.987 回答