I've fought posting this, since it seems like such a basic thing, but after 3 days of fighting with it, I'm not getting anywhere. Either my keywords trying to find a solution in the archives and via google are poor, or sleep deprivation is really taking it's toll.
We have a very basic Struts-based web application. I have a form that is used to construct an instance of our mapped object, which is then used to Query-By-Example. We want to display the results of the query in tabular form on a second page.
Looking at detailed hibernate logs, the query builds correctly, and appears to run and retrieve the correct rows. The problem occurs when we try to access the results in the list. I think this has to do with lazy initialization of the results, but am not sure how to remedy the situation.
Initially, we passed the List returned from the Query.list() method to the results page. On that page, we used the struts:logic taglib to iterate through the list. Depending on query criteria, the page would either work properly, or fail due to encountering a null inside the collection.
I tried to figure out how to set lazy initialization to false via XDoclet, but have come up empty. I added the following to the class-level @hibernate tag, but it did not seem to have any effect:
@hibernate.class table="MY_TABLE" lazy="false"
I thought I had read that objects would be initialzed from a list of references if list.get() was explicitly called for the object. I tried the code below after the query where I manually go through the list and call get(i) for each element in the set. When looking at the log output, I get a few, but not all the rows, with the majority returned as NULL. For example on a query that should return a max of 10,000 rows out of a result set of 10,018, the returned list shows size=10,000, but stepping through the list results in 438 objects, the rest of the elements return null. Looking at the logs, the hibernate extraction code indicates there are values for all of the rows, so I'm not sure why they don't make it into the collection. It almost looks like there is a timing issue since modifying the parameters sometimes allow the set to return properly, yet other times a query that just ran correctly will fail with null rows.
I tried switching to a StatelessSession object to see if that helped, but got the same result.
Is there any way for me to specify that all elements should be immediately constructed from the result set? Is there a different way I should be approaching this?
I'm sorry if this has been covered in depth already, but I'm not finding in in my searches (or in the Hibernate in Action book) and am running out of time on this. It seems to be a trivial example, so any help in getting me back on track is GREATLY appreciated.
Note: I also have a Native SQL query that shows the same behavior.
Hibernate version: 3.1.3
Mapping documents:
Class mapping files generated by XDoclet
[i]hibernate.cfg.xml[i]
<hibernate-configuration>
<session-factory>
<property name="connection.datasource">jdbc/myDS</property>
<mapping resource="foo/bar/MyObject.hbm.xml" />
</session-factory>
</hibernate-configuration>
Code between sessionFactory.openSession() and session.close():
Session session =
HibernateUtil.getSessionFactory().openSession();
Criteria criteria = session.createCriteria(MyObject.class);
criteria.add(
Example.create(quote).setPropertySelector(
new NonNullPropertySelector()));
criteria.add(
Expression.between(
"key.eventTimestamp",startTime,endTime));
criteria.setFirstResult(offset);
criteria.setMaxResults(size);
criteria.addOrder(Order.asc("key.eventTimestamp"));
List result = criteria.list();
List results =
QueryManager.queryByExample(
session,quote,startTime,endTime,offset,size);
for(int i=0; i<results.size(); i++) {
logger.debug("Row: "+results.get(i));
}
mmForm.setResultsSize(results.size());
req.setAttribute("MMQUOTE_RESULTS",results);
Full stack trace of any exception that occurs:
Name and version of the database you are using:
Oracle 10g
|