Hibernate 视图中延迟加载的问题

发布于 2024-12-05 00:06:31 字数 1362 浏览 3 评论 0原文

我在我的 Web 应用程序中部署了 @Stateless ejb。此 Web 应用程序部署在 glassfish V3.0.1 中。

我的域模型由两个类组成:MasterSlave,master 与许多 Slave 关联。

我有一个 jsf 视图 MyView,在我的 ejb MyEjb 上调用方法 displaySlaves

当在我的 ejb 的方法 displaySlaves 中时无状态 ejb MyEjb 我这样做,一切正常:

    Master master = getEntityManager().find(Master.class, 0L);
    Set<Slave> slaves = master.getSlaves();
    System.out.println("Master : " + master + " and slaves "+ slaves.size());

但是如果我尝试从我的视图 MyView 获取 master 上的引用(MyEjb 返回仅主实例,但不调用 master.getSlaves()),然后在 MyView 中,我在返回的主实例上调用 getSlaves() 我得到:

org.hibernate.LazyInitializationException:失败延迟初始化Slave集合:没有会话或者会话关闭。

显然当ejb的线程退出时,会话就关闭了。这就是错误的原因。

但是当我使用 eclipseLink 而不是 hibernate 时,一切都很好,我没有会话问题。退出 ejb 时,EclipseLink 不会关闭事务。与在 ejb 方法中获取模型实例的所有关联对象相比,对浏览返回的模型实例的视图进行编码要容易得多。

有人有解释或解决方法使其与 Hibernate 一起使用吗?我之前的问题只是一个例子,实际上我有一个带有许多映射类的遗留应用程序,并且重构注释以使其与 eclipseLink 一起工作似乎是一项艰巨的任务。

任何帮助表示赞赏。

组件:

  • Glassfish 3.0.1
  • windows 7 64 位 JDK Sun 1.6.0.24
  • Hibernate 3.5.6-FinaltiredOldDevelopper
  • v1 Final - 褪色

I have @Stateless ejb's deployed in my web application. This web application is deployed in glassfish V3.0.1

My domain model is made of two classes : Master and Slave, the master being associated to many slaves.

I have a jsf view MyView, calling the method displaySlaves on my ejb MyEjb

When in the method displaySlaves of my stateless ejb MyEjb i do this, all works fine :

    Master master = getEntityManager().find(Master.class, 0L);
    Set<Slave> slaves = master.getSlaves();
    System.out.println("Master : " + master + " and slaves "+ slaves.size());

But if i try from my view MyView to get a reference on master (MyEjb returns only the master instance, but doesn't call master.getSlaves()) and then in MyView i call getSlaves() on the returned master instance i get :

org.hibernate.LazyInitializationException: failed to lazily initialize a collection of Slave : no session or session closed.

Obviously when the thread exits of the ejb, the session is closed. That is the reason of the error.

But when i use eclipseLink instead of hibernate, all is fine, i have no session problem. EclipseLink doesn't close the transaction when exiting the ejb. That is much more easy to code the view browsing the returned model instance, than getting all associated objects of the model instance in the ejb method.

Does anybody has an explanation or a workaround to make it work with Hibernate ? My previous problem is just an illustration, and in real I have a legacy application with many many mapped classes , and the refactoring of annotations required to make it work with eclipseLink seems an huge task.

Any help appreciated.

Components :

  • Glassfish 3.0.1
  • windows 7 64 bits JDK Sun 1.6.0.24
  • Hibernate 3.5.6-Final
  • tiredOldDevelopper v1 final - fading.

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

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

发布评论

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

评论(1

凌乱心跳 2024-12-12 00:06:31

您所描述的内容多年来一直困扰着开发人员。有传言说 Gavin King(Hibernate 的创建者)开发 JBoss Seam 主要是为了为视图中的延迟加载提供一个干净的解决方案;-)

几个选项:

What you are describing is bugging developers since years. Rumors say that Gavin King (creator of Hibernate) developed JBoss Seam mainly to provide a clean solution for lazy loading in views ;-)

Couple of options:

  • disable lazy loading in your JPA metadata (FetchType.EAGER)
  • read about the infamous "open session in view pattern", but don't use it
  • if you need lazy loading in your views, consider putting an extended persistence context in the CDI conversation scope (that's what I would prefer)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文