Hibernate LazyInitializationException:未能延迟初始化角色集合

发布于 2024-09-28 11:40:54 字数 888 浏览 0 评论 0原文

我有一个 Web 服务,它本质上是 DAO 的包装器。我正在调用 Web 服务/DAO 来请求实体集合。

“父”实体类包含“子”实体对象的集合,即一对多关系。获取“父”实体集合(即 myDAO.findAll())的 DAO 方法调用没有问题地返回,但是 Web 服务调用的最终结果是这个异常:

org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: Station.observations, no session or session was closed

一些背景信息:

我有两个实体,Station 和 Observations 。 Station 实体(“父级”)包含观察对象(“子级”)的集合。

Station 实体的 DAO 类 StationDao 对于被调用的方法 findAll() 具有以下代码:

private SessionFactory sessionFactory;

private Class<T> persistentClass;

public List<T> findAll()
{
    return getCurrentSession().createQuery("from " + persistentClass.getName()).list();
}

在这种情况下,我对在父母的集合被退回。然而,似乎有人尝试在返回响应中的父(站)对象集合之前完全获取这些子(观察)对象,但由于原始会话不再可用,所以失败了。

谁能建议我如何解决这个错误?也许有一种方法可以在请求的整个生命周期中保留会话?

预先感谢您的帮助!

——詹姆斯

I have a web service which is essentially a wrapper for a DAO. I am calling the web service/DAO to request a collection of entities.

The "parent" entity class contains a collection of "child" entity objects, i.e. a one-to-many relationship. The DAO method call which fetches the "parent" entity collection (i.e. myDAO.findAll()) returns with no problem, however the final result of the web service call is this exception:

org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: Station.observations, no session or session was closed

Some background information:

I have two entities, Station and Observations. A Station entity (the "parent") contains a collection of Observations objects (the "children").

The DAO class for Station entities, StationDao has the following code for the method being called, findAll():

private SessionFactory sessionFactory;

private Class<T> persistentClass;

public List<T> findAll()
{
    return getCurrentSession().createQuery("from " + persistentClass.getName()).list();
}

In this case I'm not interested in the collection of children (observations) related to the parents (stations) being fully fetched before the collection of parents is returned. However it seems that there is some attempt being made to fully fetch these child (observations) objects before returning the collection of parent (station) objects in the response, and this is failing because the original session is no longer available.

Can anyone suggest how I would get around this error? Perhaps there's a way to persist the session over the life of the request?

Thanks in advance for your help!

--James

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

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

发布评论

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

评论(1

迎风吟唱 2024-10-05 11:40:54

我遇到过这样的情况:“无法延迟初始化角色集合”是由于调用后使用持久对象引起的

HibernateTemplate.clear()

即使会话仍然处于活动状态,但旧对象却不是。解决该问题的一种可能方法是再次从会话中获取对象。

I have had situation when "failed to lazily initialize a collection of role" was caused by using persistent objects after calling

HibernateTemplate.clear()

Even though the session was still active, the old objects were not. One possible way of solving the problem was to get the objects from session again.

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