Hibernate 从另一个会话获取 Collection
您好,我收到了臭名昭著的“没有会话或会话已关闭”的消息,我知道这是因为我试图在另一个会话中调用休眠持久对象的集合,那么有没有一种方法可以实际执行此操作而无需创建过滤器并将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
最好刷新持久化对象。例如,调用 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.发生这种情况是因为原始会话已关闭。在将项目存储到内存中之前,以及在关闭 Hibernate 会话之前,您需要访问稍后将使用的任何链接集合,以便将其加载到内存中。另一种方法是将集合设置为急切加载,但这可能会对性能产生影响。
手动预加载集合甚至可以通过访问
.Count
属性轻松完成(C#,但同样适用于 Java)。例子: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:session.merge 可能会有所帮助,具体取决于您的具体情况
session.merge MIGHT help depending on your exact situation