I am getting a lazyInitializationException whenever I access a child objects' parentObject property defined using a
many-to-one relationship in the child mapping file (see childObj.hbm.xml). I access my child object using a DAO method (see the DAO below) which uses the session.get(child.class,childId) method to return my childObj object. I am able to access the parent object and its properties using this child object only the first time!. The second time I try to use the same method it throws the lazyInitializationException
for no reason I can understand!
Interestingly, if I include a simple print statement in the DAO method which prints the parents property before returning
the child object, the lazyInitializationException does not occur any more also allowing access to the parent object properties ???
I do not want to use a eager fetching strategy as this DAO and the method that returns the child object is a generic method
used by other classes and might rarely need to access the parent object property.
A print statement in the action class that calls the DAO shows the session is available although the error message:
SEVERE: could not initialize proxy - no Session
Also, I have no idea why the print statement in the DAO is able to run that extra query to fetch the parent
while accessing the parent property whereas without the print statement directly accessing the parent objects
properties results in a lazyInitializationException.
My mapping file property and may-to-one relationships:
childObj.hbm.xml
Code:
<property name="recordDeletedBy" column="record_deleted_by"/>
<property name="recordDeletedDate" column="record_deleted_date_time"/>
<many-to-one name="parent1" column="parent1_id" class="Parent1" unique="true" />
<many-to-one name="parent2" column="parent2_id" class="Parent2" unique="true" />
SomeAction.java
Code:
public ActionForward populateDetails(ActionMapping mapping,
ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception
{
SomeActionForm rForm = (SomeActionForm) form;
if (rForm.getSelectedItem() != null) {
RsAwardDAO aDao = new RsAwardDAOImpl();
ChildObj child = childDao.findChild(someChildId);
/* This statement shows that the session is available as this statement prints as true*/
Session session = ConnectDAO.getSessionInstance();
System.out.println("Is session open: " + session.isOpen());
/* here below child.getParent1().getParentName() throws the lazyInitializationExpcetion
from the second time onwards if there is no print statement in the SomeDAOImpl.java */
try {
if (!child.getParent1() != null) {
rForm.setRsTypeName(child.getParent1().getParentName());
}
} catch (LazyInitializationException lie) {
lie.printStackTrace();
}
}
return mapping.findForward("success");
}
and finally the DAO that returns the child object
SomeDAOImpl.java
Code:
public ChildObj findChild(Long childId)
{
try
{
Session session = ConnectDAO.getSessionInstance();
ChildObj child = (ChildObj) session.get(ChildObj.class, childId);
/* If I include a simple print statement to access the parent object property here,
not only does it print properly, I do not have lazyInitializationException problems accessing
in the calling class outside this DAO method */
if (child.getParent1().getParentName() != null)
System.out.println("Parent name: "
+ child.getParent1().getParentName()
+ " in the SomeDAOImpl findChild() method");
return child;
} catch (HibernateException ex)
{
System.out.println("ERROR: Datbase error occured");
ex.printStackTrace();
}
return null;
}
Aug 4, 2008 4:15:45 PM org.hibernate.LazyInitializationException <init>
SEVERE: could not initialize proxy - no Session
org.hibernate.LazyInitializationException: could not initialize proxy - no Session
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:57)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:111)
at org.hibernate.proxy.pojo.cglib.CGLIBLazyInitializer.invoke(CGLIBLazyInitializer.java:150)
at database.Parent1$$EnhancerByCGLIB$$21b8203f.getParent1(<generated>)
at actions.rs.someAction.populateDetails(someAction.java:371)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:269)
at org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:170)
at org.apache.struts.chain.commands.servlet.ExecuteAction.execute(ExecuteAction.java:58)
at org.apache.struts.chain.commands.AbstractExecuteAction.execute(AbstractExecuteAction.java:67)
at org.apache.struts.chain.commands.ActionCommandBase.execute(ActionCommandBase.java:51)
at org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:190)
at org.apache.commons.chain.generic.LookupCommand.execute(LookupCommand.java:304)
at org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:190)
at org.apache.struts.chain.ComposableRequestProcessor.process(ComposableRequestProcessor.java:283)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:462)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:214)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:198)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:152)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:137)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:118)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:102)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:929)
at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:160)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:799)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:705)
at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:577)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Thread.java:619)