Hi, constantly I have a problem of throwing LazyInitializationException.
Either if I use a Spring or low-level comunication with database the mentioned exception is thrown whenever
I try to retrieve an object using -- load(Class klass, id) --.
If I try to retrieve using createQuery(...) method it's than ok, no exception occurs.
Here is the portion of code where I try to retrieve an object using Spring integration :
Code:
package boby;
import org.apache.commons.logging.*;
import org.hibernate.*;
import org.hibernate.cfg.Configuration;
import java.util.*;
import org.springframework.orm.hibernate3.*;
import org.springframework.context.support.*;
public class SpringEvent {
private static Log log = LogFactory.getLog(SpringEvent.class);
public static void main(String[] args) {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
ImprovedEventDao eventDao = (ImprovedEventDao) ctx.getBean("eventDao", ImprovedEventDao.class);
Event event = new Event();
event.setName("TestUnit");
eventDao.create(event);
//Event obj = (Event) result.get(0);
Event obj = (Event) eventDao.findE(event.getId()); // addition, which requre Dao changing
log.warn("Using improved Dao-- first event : " +event.getName());
log.warn("Using improved Dao-- second event : " +obj.getName());
eventDao.deleteD(event);
eventDao.deleteD(obj);
}
}
Where ImprovedEventDao extends abstract class AbstractSpringDao, which extends HibernateDaoSupport :
Code:
package boby;
import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
public abstract class AbstractSpringDao
extends HibernateDaoSupport{
public AbstractSpringDao() { }
protected void saveOrUpdate(Object obj) {
getHibernateTemplate().saveOrUpdate(obj);
}
protected void delete(Object obj) {
getHibernateTemplate().delete(obj);
}
protected Object find(Class clazz, Long id) {
return getHibernateTemplate().load(clazz, id);
}
protected List findAll(Class clazz) {
return getHibernateTemplate().find(
"from " + clazz.getName());
}
}
In low level db comunication, my method for storing and retrieving object is :
Code:
public void create(Event event)
throws DataAccessLayerException {
try {
startOperation();
session.save(event);
session.flush();
tx.commit();
} catch (HibernateException e) {
handleException(e);
} finally {
HibernateFactory.close(session);
}
}
public Event find(Long id) throws DataAccessLayerException {
Event event = null;
try {
startOperation();
event = (Event) session.load(Event.class, id);
tx.commit();
} catch (HibernateException e) {
e.printStackTrace();
log.warn("e.getMessage()");
handleException(e);
log.warn("Proba iz catcha");
} finally {
HibernateFactory.close(session);
}
return event;
}
where startoperation() mmethond perform obtaining SessionFactory, Sesion and Transaction. Also I get the mentioned
Exception. What is the problem? If I not close session after inserting data, and not commit second startoperation() and
commit() method, I do not get an exception, neither if I use createQuery() method.. But then, two distinct operation occurs during the same session!?
If I use Spring, there is no choice, and each operation will occurs in context of particular Session and Transaction.
so what is the problem!?!?