-->
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.  [ 4 posts ] 
Author Message
 Post subject: org.hibernate.SessionException:collections cannot be fetched
PostPosted: Thu Jul 16, 2009 5:43 pm 
Regular
Regular

Joined: Tue Oct 10, 2006 2:21 pm
Posts: 58
I was trying to write a simple batch process using the Stateless Session as per the Hibernate manual using the technique in section 13.3 of the Hibernate 3.2 manual.

However, I still get an exception when I try to execute a query on that object in the stateless session. This object does have a <set> property and that property is defined with lazy=false. However, I have no need of the collection in the batch process and would rather it NOT be loaded, while in the application I want eager loading and don't wish to change that. I find that there is no FetchMode I can apply to the query that avoids this exception.

Code:
Exception:
2009-07-16 17:18:15,826 [Timer-2] ERROR dao.VerificationStateAgingDao  - HibernateException
org.hibernate.SessionException: collections cannot be fetched by a stateless session
        at org.hibernate.impl.StatelessSessionImpl.initializeCollection(StatelessSessionImpl.java:226)
        at org.hibernate.collection.AbstractPersistentCollection.forceInitialization(AbstractPersistentCollection.java:454)
        at org.hibernate.engine.StatefulPersistenceContext.initializeNonLazyCollections(StatefulPersistenceContext.java:844)
        at org.hibernate.loader.Loader.loadSingleRow(Loader.java:294)
        at org.hibernate.impl.ScrollableResultsImpl.prepareCurrentRow(ScrollableResultsImpl.java:231)
        at org.hibernate.impl.ScrollableResultsImpl.next(ScrollableResultsImpl.java:100)
...


And the code:

Code:
      Criteria criteria = sess.createCriteria(User.class)
         .add(Restrictions.lt("currentVerificationStateTime", stalenessDate))
         .add(Restrictions.eq("currentVerificationState", srcState))
         .setFetchMode("addresses", FetchMode.SELECT);
   
      ScrollableResults users = criteria.scroll(ScrollMode.FORWARD_ONLY);
      return users;


Hibernate version = 3.2
Database = MYSQL 4.1.22

Is there a way to use a StatelessSession with a class with a collection member defined with "lazy=false" (or ignore the "lazy=false")? That would be my preferred route. Alternatives would be SQL queries or writing a different mapping file for the batch process, neither of which I'm totally happy with.


Top
 Profile  
 
 Post subject: Re: org.hibernate.SessionException:collections cannot be fetched
PostPosted: Thu Jul 16, 2009 6:07 pm 
Regular
Regular

Joined: Tue Oct 10, 2006 2:21 pm
Posts: 58
To expand on this a little, it would seem that defining a collection in a mapping with lazy=false preempts all opportunities to turn that off. Whereas with lazy=true (default), you can use FetchModes to dynamically override. My question appears to boil down to whether or not it is possible somehow to override in the other direction, that is to use a FetchMode (or something else) to force laziness when the normal (i.e. mapping-defined) case is non-lazy.


Top
 Profile  
 
 Post subject: Re: org.hibernate.SessionException:collections cannot be fetched
PostPosted: Wed Jul 21, 2010 6:50 pm 
Regular
Regular

Joined: Tue Feb 19, 2008 6:05 pm
Posts: 82
Any solutions yet?

stateless session with explicit fetch mode lazy on a collection still throws the exception.


Top
 Profile  
 
 Post subject: Re: org.hibernate.SessionException:collections cannot be fetched
PostPosted: Thu Jul 22, 2010 4:19 am 
Beginner
Beginner

Joined: Tue Jun 30, 2009 7:05 am
Posts: 29
Location: Italy
I have been struggling with this the last days.
What I understood is that using lazy collections means that you must have (an open session and) an open transaction when you call the getter / setter.
I had problems in adding / removing / fetching collections mapped as many-to-many and marked as lazy.
I solved by avoiding the calls to the getters where I could, or being very carefull to have an active transaction in other case.
I hope this can help you.
If you (or anyone else) have a better solution, let me know.

Stefano


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.