hibernate - 从 sessionFactory.openSession() 休眠、监听或覆盖 opneSession
问题描述
问题是,如果你使用
properties.put(Environment.CURRENT_SESSION_CONTEXT_CLASS, "thread");
然后 Hibernate 将只sessionFactory.getCurrentSession()
调用threadlocal
而不sessionFactory.openSession()
调用。
getCurrentSession
被委派给CurrentSessionContext
但openSession
没有,他们也不注册。
如果有人也试图注册这些,这将是有问题的。我们想使用ThreadLocal
List
wheregetCurrentSession
将使用堆栈中的最后一个。
当前的实现似乎认为没有必要管理 openSession 调用,因为它们无法预见其他一些潜在用途(更正确的用途)。
那么,我是要重写实体SessionFactoryImpl
类来挂钩,还是有一些事件被触发并且我可以在 Session 打开时挂钩?
解决方案
查看源代码,没有办法插入openSession
. 您可以将会话工厂包装在 aSessionFactoryDelegatingImpl
中以减少样板。
挂钩getCurrentSession
要容易得多,您只需要实现一个自定义org.hibernate.context.CurrentSessionContext
.
推荐阅读
- javascript - Javascript日期比较不包含年份
- swift - 单击分段时如何将文本字段扩展到全宽?
- javascript - 作为参数传递的异步函数代替 Promise,显示不同的结果
- python - Python 的请求没有从 Google 新闻中抓取正确的内容
- python - 有没有更有效的方法来计算距离矩阵?
- python - 如何编写python全局变量
- c - 用户实现的内存管理
- assembly - 使用 addwf pic18f2x 向 PCL 添加值时重置 PCH
- ms-access - 将表存储为动态集类型的记录集
- string - 这个 x86 汇编代码如何创建字符串?