-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: unreferenced collection error
PostPosted: Sun Jan 13, 2008 11:26 pm 
Newbie

Joined: Sun Jan 13, 2008 10:49 pm
Posts: 3
I'm trying out hibernate for the first time and I've run into a problem that I can't figure out. I'm trying to load a portion of a collection through a collection filter and I keep getting this exception:

Code:
Request processing failed; nested exception is org.hibernate.QueryException: The collection was unreferenced
Caused by: org.hibernate.QueryException: The collection was unreferenced
   at org.hibernate.impl.SessionImpl.getFilterQueryPlan(SessionImpl.java:1437)
   at org.hibernate.impl.SessionImpl.createFilter(SessionImpl.java:1247)
   at ...


The weird thing is that I don't always get this exception. I'll explain more after my code. I've trimmed it down to just the applicable sections.

Code:
public class Tag implements Serializable{
  @ManyToMany(fetch=FetchType.LAZY)
  private List<Writing> taggedByThis;
}


public class Writing implements Serializable{
  @ManyToMany(fetch=FetchType.EAGER, mappedBy="taggedByThis")
  private List<Tag> descriptionTags;
}


public List getWritingsByTag(int numResults, int firstResult, Tag tag){
  Query query = sessionFactory.getCurrentSession().createFilter(tag.getTaggedByThis(), "order by votes desc");
  query.setFirstResult(firstResult);
  query.setMaxResults(numResults);
  List l = query.list();
  return l;
}


The function getWritingsByTag() is loading the Tag object's collection of Writings via pagination. When I initially run my web app and getWritingsByTag() is called with values of firstResult=0 and numResults=15, the call is successful. The page can then be refreshed, revisited, etc. without error. But when I go to the next "page" of results and getWritingsByTag() is called with firstResult=15, the above exception is thrown. This behavior occurs every time.

Another interesting behavior is that if the user visiting my site visits page 2 first (firstResult=15), the function completes successfully (and the page can be refreshed and revisited without problem). If the same user then tries to go back to page one (firstResult=0), the exception gets thrown.

I've found other posts with people who are getting the same exception, but I don't think it's for the same reason. I haven't found any posts where the collection filter works on one "page" but not on the next.

Any ideas?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 15, 2008 10:02 pm 
Newbie

Joined: Sun Jan 13, 2008 10:49 pm
Posts: 3
BTW, I have seen the section in the Hibernate FAQ regarding this exception:

Quote:
I tried to filter a query result List and recieved net.sf.hibernate.QueryException: The collection was unreferenced.

Collection filters are for filtering persistent collections, not query results.


I don't see how that answer helps as I am using a persistent collection.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 16, 2008 10:57 am 
Newbie

Joined: Sun Jan 13, 2008 10:49 pm
Posts: 3
Well, I figured out the cause of the problem, but I'm sure how to fix it. The root of the problem lies in caching.

Code:
@Cacheable (modelId="myCacheModel")
  public Tag getTag(String tagName){
    Object ret = sessionFactory.getCurrentSession().load(Tag.class, tagName);
    return (Tag)ret;
  }


The above function caches the tag object (I'm using ehcache) that is being used to populate the loaded page (via getWritingsByTag function). So here is what I think is happening:

-The user loads page 1 for the tag "someTagName".
-getTag(someTagName) is called which fetches the tag from the database, loads it, caches it, and returns it as someTagObject.
-getWritingsByTag(0, 10, someTagObject)
-The page is populated with the results of the getWritingsByTag() and everything works fine.
-The user loads page 2 for the tag "someTagName".
-The collection someTagObject.getTaggedByThis() goes out of scope (something to do with the way hibernate handles persistent collections by wrapping them in a PersistentCollection object)
-getTag(someTagName) is called which gets intercepted and redirected to cache which returns someTagObject
-getWritingsByTag(10, 10, someTagObject) is called
-Query query = sessionFactory.getCurrentSession().createFilter(tag.getTaggedByThis(), "order by votes desc"); creates an exception because tag.getTaggedByThis() is unreferenced.

I'm still not sure why all of the above is happening the way it is. It seems like hibernate would be smart enough to try to load the collection from the database if it ever becomes "unreferenced". The entire problem goes away if I disable caching on the getTag() function. This is obviously undesirable because it would incur a performance hit.

Has anyone else run into this problem?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.