在会话关闭后,如何使用hibernate访问延迟加载的字段? [英] How can I access lazy-loaded fields after the session has closed, using hibernate?

查看:100
本文介绍了在会话关闭后,如何使用hibernate访问延迟加载的字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑这种情况:


  • 我通过hibernate加载了一个Parent实体

  • 父包含一个大型且延迟加载的子集合

  • 在用户查看父数据的初始加载后,休眠会话被关闭

  • 用户可以选择查看lazy Children集合的内容

  • 我现在希望加载该集合



<




  • 假设session-in-view不是一个选项,因为获取只有在用户查看父母并决定查看孩子后,才能收集儿童收藏。

  • 这是一项服务,可通过基于Web和桌面的客户端远程访问。
  • >


谢谢。

解决方案

对用户看什么做了一些假设,但似乎只有你自己想要检索孩子,如果用户已经查看父母,并真的想看到孩子。



为什么不尝试打开一个新的会话,并由他们的父母? ... $ / $>
$ b $

  criteria = session.createCriteria(Child.class); 
criteria.add(Restrictions.eq(parent,parent));
列表< Child> children = criteria.list();


consider this scenario:

  • I have loaded a Parent entity through hibernate
  • Parent contains a collection of Children which is large and lazy loaded
  • The hibernate session is closed after this initial load while the user views the Parent data
  • The user may choose to view the contents of the lazy Children collection
  • I now wish to load that collection

What are the ways / best way of loading this collection?

  • Assume session-in-view is not an option as the fetching of the Children collection would only happen after the user has viewed the Parent and decided to view the Children.
  • This is a service which will be accessed remotely by web and desktop based client.

Thanks.

解决方案

I'm making some assumptions about what the user is looking at, but it seems like you only want to retrieve the children if the user has already viewed the parent and really wants to see the children.

Why not try opening a new session and fetching the children by their parent? Something along the lines of ...

criteria = session.createCriteria(Child.class);
criteria.add(Restrictions.eq("parent", parent));
List<Child> children = criteria.list();

这篇关于在会话关闭后,如何使用hibernate访问延迟加载的字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆