OpenEntityManagerInView 相当于 Junit 和 Quartz 作业

发布于 2024-11-29 05:24:11 字数 659 浏览 9 评论 0原文

是否有任何标准方法(或替代在视图中打开会话[EntityManager])来使用依赖于实体的延迟初始化(fetch = FetchType.LAZY)的类 以下两个用例:

  1. 在 JUnit 测试用例中

  2. 在quartz 作业中

对于上述两种情况,OSIV 模式将无法工作,因为没有 Web 请求进入系统。在这种情况下如何实现延迟加载?

PS:Spring的OpenEntityManagerInViewFilterOpenEntityManagerInViewInterceptor都是面向Web请求的。

  1. 我无法通过执行虚拟 get 来预取所有此类关系/实体,只是为了在存储库(DAO)中关闭会话之前加载它。
  2. 我知道,对于 JUnit,我可以在 @Before / @after 中手动打开/关闭 EM。但我正在寻找更优雅和标准的东西。

参考资料: 由于“父”应用程序上下文中没有定义会话,导致出现 LazyInitializationException在 Hibernate 3 中使用注释的模块化 Spring 应用程序

Is there any standard way (or substitute to Open Session [EntityManager] in View) for usage of classes that depend on Lazy Initialization (fetch = FetchType.LAZY) of entities in
following two use-cases:

  1. In the JUnit test cases

  2. In quartz jobs

For both the above scenarios OSIV pattern will not work as no web request enters the system. How to get Lazy Loading work in such situations?

PS: Spring's OpenEntityManagerInViewFilter and OpenEntityManagerInViewInterceptor are both web request oriented.

  1. I can not prefetch all such relations/entites by doing a dummy get just to load it before session is closed in the repository(DAO).
  2. I know, for JUnit I can manually open/close EM in @Before / @after. But am looking for something more elegant and standard.

Rerference :
LazyInitializationException due to no session defined in "parent" application context in Hibernate 3 in a moduled Spring application using annotations

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苏辞 2024-12-06 05:24:11

交易就是您所需要的。打开事务,获取数据,对其进行操作,然后在完成后关闭事务。 Spring 将 EntityManager 生命周期绑定到事务的生命周期,并根据需要保持其打开状态。

Spring 事务管理本身就是一个重要的主题,但使用它确实非常简单。请参阅文档

Transactions are what you need. Open a transaction, fetch the data, manipulate it, then close the transaction when you're done. Spring binds the EntityManager lifecycle to that of the transaction, and holds it open as long as you need.

Spring Transaction Management is a significant subject in itself, but using it is really quite straightforward. See the docs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文