首页 > 解决方案 > 从 sessionFactory.openSession() 休眠、监听或覆盖 opneSession

问题描述

问题是,如果你使用

properties.put(Environment.CURRENT_SESSION_CONTEXT_CLASS, "thread");

然后 Hibernate 将只sessionFactory.getCurrentSession()调用threadlocal而不sessionFactory.openSession()调用。

getCurrentSession被委派给CurrentSessionContextopenSession没有,他们也不注册。

如果有人也试图注册这些,这将是有问题的。我们想使用ThreadLocal ListwheregetCurrentSession将使用堆栈中的最后一个。

当前的实现似乎认为没有必要管理 openSession 调用,因为它们无法预见其他一些潜在用途(更正确的用途)。

那么,我是要重写实体SessionFactoryImpl类来挂钩,还是有一些事件被触发并且我可以在 Session 打开时挂钩

标签: hibernatetransactions

解决方案


查看源代码,没有办法插入openSession. 您可以将会话工厂包装在 aSessionFactoryDelegatingImpl中以减少样板。

挂钩getCurrentSession要容易得多,您只需要实现一个自定义org.hibernate.context.CurrentSessionContext.


推荐阅读