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.