Hi All
I have a question on lazy loading using HibernateTemplate in a Struts
app - see below:
Deal is a business object that has a collection. If I use the get
method as commented below, lazy loading fails when retrieving the
collection from the Struts tag. However, using a Query as below allows
me to do lazy load.
I do not understand why the code below works - the hibernate session
should have been closed when it got to the Struts rendering stage.
public Deal findById(int dealId) {
// return (Deal) this.getHibernateTemplate().get(Deal.class,
new Integer(dealId));
Query query = null;
try {
query = this.getHibernateTemplate().createQuery(this.getSession(),
"FROM Deal AS D " +
"WHERE D.id=" + dealId
);
Deal deal = (Deal) query.uniqueResult();
return deal;
}
catch (HibernateException eHibernate) {
log.error("Error in findById: " + eHibernate.getMessage());
}
return null;
}
Thanks
F
|