I am creating a job portal admin. I am using spring + hibernate. I have created page for add job, it is working fine, but when i am using the same page for edit of jobs , it is giving me exception:
org.hibernate.LazyInitializationException: could not initialize proxy - no Session
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:132)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:174)
at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:190)
at com.jobs.admin.data.Company_$$_javassist_5.toString(Company_$$_javassist_5.java)
at org.springframework.util.ObjectUtils.nullSafeToString(ObjectUtils.java:560)
at org.springframework.util.ObjectUtils.getDisplayString(ObjectUtils.java:506)
at org.springframework.web.servlet.tags.form.SelectedValueComparator.exhaustiveCompare(SelectedValueComparator.java:178)
at org.springframework.web.servlet.tags.form.SelectedValueComparator.isSelected(SelectedValueComparator.java:103)
at org.springframework.web.servlet.tags.form.OptionTag.isSelected(OptionTag.java:243)
at org.springframework.web.servlet.tags.form.OptionTag.renderOption(OptionTag.java:211)
at org.springframework.web.servlet.tags.form.OptionTag.renderFromBodyContent(OptionTag.java:161)
at org.springframework.web.servlet.tags.form.AbstractHtmlElementBodyTag.doEndTag(AbstractHtmlElementBodyTag.java:66)
at org.apache.jsp.WEB_002dINF.jsp.admin.addJob_jsp._jspService(addJob_jsp.java:735)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:654)
at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:445)
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:379)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:292)
at org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:238)
at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:250)
at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1060)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:798)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:716)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:647)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:552)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:261)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:581)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run
(Thread.java:619)
My controller class is having two method, one for adding job and one for edit that job. Why this is happening , please give some suggestions.
Why this is happening:
Looks like that in a JSP the toString
method of Company
is invoked. This triggers a (company) object to load (it is only layz loaded). Because you do not have a hibernate session in the JSP you get this error.
Give some suggestions:
OpenSessionInViewFilter
or OpenEntityManagerInViewFilter
Your entity (com.jobs.admin.data.Company
) is lazily initialized. Meaning that some members were not really initialized, but Hibernate put a proxy instead and it is going to put the real values on demand.
After the entity is loaded from the database, you close the Session, so the members that were proxy-ed, were not initialized! And now you call com.jobs.admin.data.Company.toString()
that uses members that were proxy-ed. So you get the exception.
Few suggestions:
toString()
methods. toString()
may be called quite unpredictably, for example during logging and so on. You will hardly know if you have an open session during this time or not.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With