Hibernate 从另一个会话获取 Collection

发布于 2024-10-27 08:19:39 字数 293 浏览 3 评论 0原文

您好,我收到了臭名昭著的“没有会话或会话已关闭”的消息,我知道这是因为我试图在另一个会话中调用休眠持久对象的集合,那么有没有一种方法可以实际执行此操作而无需创建过滤器并将 JOIN 获取模式添加到其中...

GRAVE:无法延迟初始化角色集合:ni.edu.uni.id.data.object.Curso.modulos,没有会话或会话被关闭 org.hibernate.LazyInitializationException:未能延迟初始化角色集合:ni.edu.uni.id.data.object.Curso.modulos,没有会话或会话被关闭

Hi i've been getting the infamous "no session or session was closed" i know that is because i am trying to invke a collection of a hibernate persisted object in another session, so is there a way to actually do this without having to create the filter and add the JOIN fetch mode into it....

GRAVE: failed to lazily initialize a collection of role: ni.edu.uni.id.data.object.Curso.modulos, no session or session was closed
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: ni.edu.uni.id.data.object.Curso.modulos, no session or session was closed

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

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

发布评论

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

评论(3

轮廓§ 2024-11-03 08:19:39

最好刷新持久化对象。例如,调用 DAO getById() 传递对象的 id,然后使用必要的集合。

It's better to refresh the persistent object. E.g. call DAO getById() passing the id of the object and then use the necessary collection.

双手揣兜 2024-11-03 08:19:39

发生这种情况是因为原始会话已关闭。在将项目存储到内存中之前,以及在关闭 Hibernate 会话之前,您需要访问稍后将使用的任何链接集合,以便将其加载到内存中。另一种方法是将集合设置为急切加载,但这可能会对性能产生影响。

手动预加载集合甚至可以通过访问 .Count 属性轻松完成(C#,但同样适用于 Java)。例子:

Brand b = BrandRepository.GetById(1234);
int tmp = b.Products.Count; //this is only to load the products collection.  This can then be used in a later session.

This happens because the original session is closed. Before storing the item in memory, and prior to the Hibernate session being closed, you need to access any linked collections that will be used later on, so that it is loaded in memory. An alternative to this is to setup the collection such that it is Eager-loaded, however this might have performance implications.

Preloading a collection manually can easily be done by even accessing the .Count property (C#, but same applies for Java). Example:

Brand b = BrandRepository.GetById(1234);
int tmp = b.Products.Count; //this is only to load the products collection.  This can then be used in a later session.
硬不硬你别怂 2024-11-03 08:19:39

session.merge 可能会有所帮助,具体取决于您的具体情况

session.merge MIGHT help depending on your exact situation

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