views:

24

answers:

0

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