Hi. I have a fairly simply bit of code that fetches a list of objects, and then for each one, accesses a collection on that object. The first query is fine, but accessing the collection fails with failed to lazily initialize a collection of role: foo, no session or session was closed.
Normally I'd think I forgot my OpenSessionInView filter and fix it. But that's not used in this case. The code is:
Code:
public
List<Mission>
getMissionList(String inDeviceID, String inUserAgent, Date inLastFetch)
{
try
{
ServletContext ctx = getServletContext();
WebApplicationContext springContext = WebApplicationContextUtils.getWebApplicationContext(ctx);
IService s = (IService) springContext.getBean("service");
Date lastRequestDate = inLastFetch;
if (inLastFetch == null)
{
DataRequest lastRequest = s.getMostRecentRequest(inDeviceID);
lastRequestDate = lastRequest.getDate();
sLogger.debug("No lastFetchDate from client, got from last request record");
}
sLogger.debug("Last fetch date: " + lastRequestDate);
// Fetch the Missions…
int numMissions = 0;
int numEvents = 0;
int numNewsItems = 0;
sLogger.info("Requesting missions since " + lastRequestDate + " for device " + inDeviceID);
List<Mission> missions = s.getMissionList(lastRequestDate);
sLogger.debug("Got " + missions.size() + " missions.");
numMissions += missions.size();
for (Mission m : missions)
{
numEvents += m.getEvents().size(); // EXCEPTION HERE
}
sLogger.info("Sent device [" + inDeviceID + "] "
+ numMissions + " missions, "
+ numEvents + " events, "
+ numNewsItems + " news items.");
return missions;
The exception occurs in getEvents().size().
Events are mapped with:
Code:
@OneToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE }, mappedBy = "mMission")
private Set<MissionEvent> mEvents = new HashSet<MissionEvent>();
Now, this used to work, but it was a while back, and I'm not sure if I changed anything. Source control shows nothing obvious. The first request works, the second does not.
For a while I thought it was because I was releasing the session in the getMissionList() method, but I tried commenting out that release, and it still happens.