Announcement Announcement Module
Collapse
No announcement yet.
Cannot catch ObjectNotFoundException in Spring 1.2.2 Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Cannot catch ObjectNotFoundException in Spring 1.2.2

    Hi , l migrating my application from (hibernate2.1 + Spring 1.2.1) to (hibernate3 + Spring 1.2.2) , meet problem in catching "org.hibernate.ObjectNotFoundException" , l cannot catch the ObjectNotFoundException using the following code ,
    Code:
    try {
                  publisher = getLibrary().loadPublisher(RequestUtils.getLongParameter(request,"id",0));
    }catch(org.springframework.orm.ObjectRetrievalFailureException orfe){
                  return new Publisher();
    }
    It used to work in hibernate2.1 + Spring 1.2.1.

    The error message ,
    2005-07-24 16:14:29,898 INFO [org.hibernate.event.def.DefaultLoadEventListener] - <Error performing load command>
    org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [org.yourschool.library.domain.Publisher#0]
    at org.hibernate.ObjectNotFoundException.throwIfNull( ObjectNotFoundException.java:27)
    at org.hibernate.event.def.DefaultLoadEventListener.l oad(DefaultLoadEventListener.java:118)
    at org.hibernate.event.def.DefaultLoadEventListener.o nLoad(DefaultLoadEventListener.java:75)
    at org.hibernate.impl.SessionImpl.immediateLoad(Sessi onImpl.java:643)
    at org.hibernate.proxy.AbstractLazyInitializer.initia lize(AbstractLazyInitializer.java:59)
    at org.hibernate.proxy.AbstractLazyInitializer.getImp lementation(AbstractLazyInitializer.java:84)
    at org.hibernate.proxy.CGLIBLazyInitializer.intercept (CGLIBLazyInitializer.java:134)
    at org.yourschool.library.domain.Publisher$$EnhancerB yCGLIB$$78e39465.toString(<generated>)
    at java.lang.String.valueOf(Unknown Source)
    at java.lang.StringBuffer.append(Unknown Source)
    at org.yourschool.library.web.PublisherForm.formBacki ngObject(PublisherForm.java:35)
    at org.springframework.web.servlet.mvc.AbstractFormCo ntroller.getErrorsForNewForm(AbstractFormControlle r.java:325)
    at org.springframework.web.servlet.mvc.AbstractFormCo ntroller.showNewForm(AbstractFormController.java:3 06)
    at org.springframework.web.servlet.mvc.AbstractFormCo ntroller.handleRequestInternal(AbstractFormControl ler.java:248)
    at org.springframework.web.servlet.mvc.AbstractContro ller.handleRequest(AbstractController.java:128)
    at org.springframework.web.servlet.mvc.SimpleControll erHandlerAdapter.handle(SimpleControllerHandlerAda pter.java:44)
    at org.springframework.web.servlet.DispatcherServlet. doDispatch(DispatcherServlet.java:684)
    at org.springframework.web.servlet.DispatcherServlet. doService(DispatcherServlet.java:625)
    at org.springframework.web.servlet.FrameworkServlet.s erviceWrapper(FrameworkServlet.java:386)
    at org.springframework.web.servlet.FrameworkServlet.d oGet(FrameworkServlet.java:346)
    at javax.servlet.http.HttpServlet.service(HttpServlet .java:689)
    at javax.servlet.http.HttpServlet.service(HttpServlet .java:802)
    at org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:237)
    at org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:157)
    at org.springframework.orm.hibernate3.support.OpenSes sionInViewFilter.doFilterInternal(OpenSessionInVie wFilter.java:172)
    at org.springframework.web.filter.OncePerRequestFilte r.doFilter(OncePerRequestFilter.java:76)
    at org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:186)
    at org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:157)
    at com.opensymphony.module.sitemesh.filter.PageFilter .parsePage(PageFilter.java:118)
    at com.opensymphony.module.sitemesh.filter.PageFilter .doFilter(PageFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:186)
    at org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:157)
    at org.yourschool.library.web.filter.ExceptionFilter. doFilter(ExceptionFilter.java:48)
    at org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:186)
    at org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:157)
    at org.yourschool.library.web.filter.EncodingFilter.d oFilter(EncodingFilter.java:43)
    at org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:186)
    at org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:157)
    at org.apache.catalina.core.StandardWrapperValve.invo ke(StandardWrapperValve.java:214)
    at org.apache.catalina.core.StandardValveContext.invo keNext(StandardValveContext.java:104)
    at org.apache.catalina.core.StandardPipeline.invoke(S tandardPipeline.java:520)
    at org.apache.catalina.core.StandardContextValve.invo keInternal(StandardContextValve.java:198)
    at org.apache.catalina.core.StandardContextValve.invo ke(StandardContextValve.java:152)
    at org.apache.catalina.core.StandardValveContext.invo keNext(StandardValveContext.java:104)
    at org.apache.catalina.core.StandardPipeline.invoke(S tandardPipeline.java:520)
    at org.apache.catalina.core.StandardHostValve.invoke( StandardHostValve.java:137)
    at org.apache.catalina.core.StandardValveContext.invo keNext(StandardValveContext.java:104)
    at org.apache.catalina.valves.ErrorReportValve.invoke (ErrorReportValve.java:118)
    at org.apache.catalina.core.StandardValveContext.invo keNext(StandardValveContext.java:102)
    at org.apache.catalina.core.StandardPipeline.invoke(S tandardPipeline.java:520)
    at org.apache.catalina.core.StandardEngineValve.invok e(StandardEngineValve.java:109)
    at org.apache.catalina.core.StandardValveContext.invo keNext(StandardValveContext.java:104)
    at org.apache.catalina.core.StandardPipeline.invoke(S tandardPipeline.java:520)
    at org.apache.catalina.core.ContainerBase.invoke(Cont ainerBase.java:929)
    at org.apache.coyote.tomcat5.CoyoteAdapter.service(Co yoteAdapter.java:160)
    at org.apache.coyote.http11.Http11Processor.process(H ttp11Processor.java:799)
    at org.apache.coyote.http11.Http11Protocol$Http11Conn ectionHandler.processConnection(Http11Protocol.jav a:705)
    at org.apache.tomcat.util.net.TcpWorkerThread.runIt(P oolTcpEndpoint.java:577)
    at org.apache.tomcat.util.threads.ThreadPool$ControlR unnable.run(ThreadPool.java:683)
    at java.lang.Thread.run(Unknown Source)
    2005-07-24 16:14:29,928 WARN [org.springframework.web.servlet.DispatcherServlet] - <Handler execution resulted in exception - forwarding to resolved error view>
    org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [org.yourschool.library.domain.Publisher#0]
    at org.hibernate.ObjectNotFoundException.throwIfNull( ObjectNotFoundException.java:27)
    at org.hibernate.event.def.DefaultLoadEventListener.l oad(DefaultLoadEventListener.java:118)
    at org.hibernate.event.def.DefaultLoadEventListener.o nLoad(DefaultLoadEventListener.java:75)
    at org.hibernate.impl.SessionImpl.immediateLoad(Sessi onImpl.java:643)
    at org.hibernate.proxy.AbstractLazyInitializer.initia lize(AbstractLazyInitializer.java:59)
    at org.hibernate.proxy.AbstractLazyInitializer.getImp lementation(AbstractLazyInitializer.java:84)
    at org.hibernate.proxy.CGLIBLazyInitializer.intercept (CGLIBLazyInitializer.java:134)
    at org.yourschool.library.domain.Publisher$$EnhancerB yCGLIB$$78e39465.toString(<generated>)
    at java.lang.String.valueOf(Unknown Source)
    at java.lang.StringBuffer.append(Unknown Source)
    at org.yourschool.library.web.PublisherForm.formBacki ngObject(PublisherForm.java:35)
    at org.springframework.web.servlet.mvc.AbstractFormCo ntroller.getErrorsForNewForm(AbstractFormControlle r.java:325)
    at org.springframework.web.servlet.mvc.AbstractFormCo ntroller.showNewForm(AbstractFormController.java:3 06)
    at org.springframework.web.servlet.mvc.AbstractFormCo ntroller.handleRequestInternal(AbstractFormControl ler.java:248)
    at org.springframework.web.servlet.mvc.AbstractContro ller.handleRequest(AbstractController.java:128)
    at org.springframework.web.servlet.mvc.SimpleControll erHandlerAdapter.handle(SimpleControllerHandlerAda pter.java:44)
    at org.springframework.web.servlet.DispatcherServlet. doDispatch(DispatcherServlet.java:684)
    at org.springframework.web.servlet.DispatcherServlet. doService(DispatcherServlet.java:625)
    at org.springframework.web.servlet.FrameworkServlet.s erviceWrapper(FrameworkServlet.java:386)
    at org.springframework.web.servlet.FrameworkServlet.d oGet(FrameworkServlet.java:346)
    at javax.servlet.http.HttpServlet.service(HttpServlet .java:689)
    at javax.servlet.http.HttpServlet.service(HttpServlet .java:802)
    at org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:237)
    at org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:157)
    at org.springframework.orm.hibernate3.support.OpenSes sionInViewFilter.doFilterInternal(OpenSessionInVie wFilter.java:172)
    at org.springframework.web.filter.OncePerRequestFilte r.doFilter(OncePerRequestFilter.java:76)
    at org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:186)
    at org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:157)
    at com.opensymphony.module.sitemesh.filter.PageFilter .parsePage(PageFilter.java:118)
    at com.opensymphony.module.sitemesh.filter.PageFilter .doFilter(PageFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:186)
    at org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:157)
    at org.yourschool.library.web.filter.ExceptionFilter. doFilter(ExceptionFilter.java:48)
    at org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:186)
    at org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:157)
    at org.yourschool.library.web.filter.EncodingFilter.d oFilter(EncodingFilter.java:43)
    at org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:186)
    at org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:157)
    at org.apache.catalina.core.StandardWrapperValve.invo ke(StandardWrapperValve.java:214)
    at org.apache.catalina.core.StandardValveContext.invo keNext(StandardValveContext.java:104)
    at org.apache.catalina.core.StandardPipeline.invoke(S tandardPipeline.java:520)
    at org.apache.catalina.core.StandardContextValve.invo keInternal(StandardContextValve.java:198)
    at org.apache.catalina.core.StandardContextValve.invo ke(StandardContextValve.java:152)
    at org.apache.catalina.core.StandardValveContext.invo keNext(StandardValveContext.java:104)
    at org.apache.catalina.core.StandardPipeline.invoke(S tandardPipeline.java:520)
    at org.apache.catalina.core.StandardHostValve.invoke( StandardHostValve.java:137)
    at org.apache.catalina.core.StandardValveContext.invo keNext(StandardValveContext.java:104)
    at org.apache.catalina.valves.ErrorReportValve.invoke (ErrorReportValve.java:118)
    at org.apache.catalina.core.StandardValveContext.invo keNext(StandardValveContext.java:102)
    at org.apache.catalina.core.StandardPipeline.invoke(S tandardPipeline.java:520)
    at org.apache.catalina.core.StandardEngineValve.invok e(StandardEngineValve.java:109)
    at org.apache.catalina.core.StandardValveContext.invo keNext(StandardValveContext.java:104)
    at org.apache.catalina.core.StandardPipeline.invoke(S tandardPipeline.java:520)
    at org.apache.catalina.core.ContainerBase.invoke(Cont ainerBase.java:929)
    at org.apache.coyote.tomcat5.CoyoteAdapter.service(Co yoteAdapter.java:160)
    at org.apache.coyote.http11.Http11Processor.process(H ttp11Processor.java:799)
    at org.apache.coyote.http11.Http11Protocol$Http11Conn ectionHandler.processConnection(Http11Protocol.jav a:705)
    at org.apache.tomcat.util.net.TcpWorkerThread.runIt(P oolTcpEndpoint.java:577)
    at org.apache.tomcat.util.threads.ThreadPool$ControlR unnable.run(ThreadPool.java:683)
    at java.lang.Thread.run(Unknown Source)
    is that my problem ? Thanks

    moon

  • #2
    The above "org.hibernate.ObjectNotFoundException" are from formBackingObject() , but it is so strange that it did not happen in PublisherDelete.java , it can be catch in PublisherDelete's handleRequestInternal(..).

    PublisherForm.java,
    Code:
    public class PublisherForm extends AbstractLibraryFormController &#123;
    
    	public PublisherForm&#40;&#41; &#123;
    	       	setCommandClass&#40;Publisher.class&#41;;
    	       	setCommandName&#40;"publisher"&#41;;
    	       	// activate session form mode to allow for detection of duplicate submissions
    	       	setSessionForm&#40;true&#41;;
    	       	//setBindOnNewForm&#40;true&#41;;
    	&#125;
    
    	/** Method forms a copy of an existing Publisher for editing */
    	protected Object formBackingObject&#40;HttpServletRequest request&#41; throws ServletException &#123;
    
                    Publisher publisher = null;
                    //System.out.println&#40;"******|||||||||*******"&#41;;
                    try &#123;
                            publisher = getLibrary&#40;&#41;.loadPublisher&#40;RequestUtils.getLongParameter&#40;request,"id",0&#41;&#41;;
                            //System.out.println&#40;"+++++++"+publisher+"+++++++"&#41;;
                    &#125;catch&#40;org.springframework.orm.ObjectRetrievalFailureException orfe&#41;&#123;
                            //System.out.println&#40;"+++++++"+orfe+"+++++++"&#41;;
                            return new Publisher&#40;&#41;;
                    &#125;
    
                    return publisher;
    
    	&#125;
    
            public ModelAndView onSubmit&#40;HttpServletRequest request, HttpServletResponse response, Object command, BindException errors&#41;
    		throws Exception &#123;
    
    		Publisher publisher = &#40;Publisher&#41; command;
                    Long id = publisher.getId&#40;&#41;;
    
                    // &#40;id == null&#41; == "Add" action , otherwise "Edit" action
    		try&#123;
    		   	getLibrary&#40;&#41;.storePublisher&#40;publisher&#41;;
    
                            if&#40;id == null&#41; &#123;
                                    writeMessage&#40;request,"message_publisher_added","publisher.added",
                                            new Object&#91;&#93; &#123;publisher.getPublisherName&#40;&#41;&#125;&#41;;
                                    request.getSession&#40;&#41;.setAttribute&#40;"addedPublisher",publisher&#41;;
                            &#125;else &#123;
                            		writeMessage&#40;request,"message_publisher_edited","publisher.edited",
                                            new Object&#91;&#93; &#123;publisher.getPublisherName&#40;&#41;&#125;&#41;;
                                    request.getSession&#40;&#41;.setAttribute&#40;"editedPublisher",publisher&#41;;
                            &#125;
    		&#125;
                    catch&#40;DataIntegrityViolationException eive&#41;&#123;
    
                            // errors.rejectValue&#40;x,y,z&#41; - z just a default , if found no y ,
                            // then display z , it will lose i18n if show z.
                            errors.rejectValue&#40;"publisherName", "publisher.name.already.exists",
                                               "Publisher Name already exists ..."&#41;;
    			return showForm&#40;request, response, errors&#41;;
    
    		&#125;catch&#40;ObjectOptimisticLockingFailureException eoolfe&#41;&#123;
    		        errors.rejectValue&#40;"publisherName", "publisher.name.already.updated.or.deleted",
                                               "The Publisher Name have been updated or deleted by other user..."&#41;;
    
                            return showForm&#40;request, response, errors&#41;;
    		&#125;
    
                    if&#40;id == null&#41;  return  new ModelAndView&#40;new RedirectView&#40;getSuccessAddView&#40;&#41;&#41;&#41;;
    
                    return new ModelAndView&#40;new RedirectView&#40;getSuccessEditView&#40;&#41;&#41;,makePagingCommandMap&#40;request,getActionName&#40;&#41;&#41;&#41;;
    
            &#125;
    
    &#125;
    PublisherDelete.java
    Code:
    public class PublisherDelete extends AbstractLibraryController &#123;
    
    	public ModelAndView handleRequestInternal&#40;HttpServletRequest request, HttpServletResponse response&#41;
                   throws Exception &#123;
    
                   try&#123;
                       
                       Publisher publisher = getLibrary&#40;&#41;.loadPublisher&#40;RequestUtils.getLongParameter&#40;request,"id",0&#41;&#41;;
    
                       getLibrary&#40;&#41;.deletePublisher&#40;RequestUtils.getLongParameter&#40;request,"id",0&#41;&#41;;
    
                       writeMessage&#40;request,"message_publisher_deleted","publisher.deleted",
                                            new Object&#91;&#93; &#123;publisher.getPublisherName&#40;&#41;&#125;&#41;;
    
                   &#125;catch &#40;org.springframework.orm.ObjectRetrievalFailureException orfe&#41;&#123;
                                                                                        	
                       System.out.println&#40;"@@@@@"+orfe+"@@@@@"&#41;;
                       return new ModelAndView&#40;new RedirectView&#40;getSuccessView&#40;&#41;&#41;, makePagingCommandMap&#40;request,getActionName&#40;&#41;&#41;&#41;;
                  &#125;
    
                  return new ModelAndView&#40;new RedirectView&#40;getSuccessView&#40;&#41;&#41;, makePagingCommandMap&#40;request,getActionName&#40;&#41;&#41;&#41;;
            &#125;
    
    &#125;
    there is an OpenSessionInViewFilter for this two controller to avoid org.hibernate.LazyInitializationException in hibernate3 .
    Any body met this problem b4 ?

    moon

    Comment


    • #3
      Default setting for class's lazy attribute in xxx.hbm.xml in Hibernate2 was false , but hibernate3 change the default to true,
      Code cause problem:
      Code:
      <class name="org.yourschool.library.domain.Publisher"
      	table="PUBLISHERS" >
      have to change it to in hibernate3
      Code:
      <class name="org.yourschool.library.domain.Publisher"
      	table="PUBLISHERS" lazy="false">
      then the problem solved (even without the OpenSessionInViewFilter).

      But l still don't understand ... why we cannot catch a lazy loading class ?, can somebody help me to understand this behaviour ..thanks in advance.

      moon

      Comment


      • #4
        I guess the reason for the behavior you're experiencing is Hibernate's lazy loading for classes: A "load" call is not necessarily actually loading the instance. It might rather expose a proxy to you that won't load its data until you touch the corresponding fields. As a consequence, the actual check for existence will happen lazily as well...

        Juergen

        Comment


        • #5
          Hi Juergen, thanks for your answer ,

          yes , l have thought this answer too , but the strange thing is , l can catch it using org.hibernate.ObjectNotFoundException instead of org.springframework.orm.ObjectRetrievalFailureExce ption ... why ?

          haha ...still don't understand ...

          moon

          Comment


          • #6
            Hibernate does not initialize your object before you touch any method other than the id accessor. In your case, that is the "toString" method that you implicitly call in your "formBackingObject" implementation, through the printing to the console. This already happens outside the Spring DAO, so now exception conversion takes place - hence, you get the raw Hibernate exception.

            I would recommend to switch Hibernate's lazy loading off - at least at the class level.

            Juergen

            Comment


            • #7
              Yes , you are right.

              Actually l cannot catch the org.hibernate.ObjectNotFoundException if comment out the system printout.
              Code:
              System.out.println&#40;"+++++++"+publisher+"+++++++"&#41;;
              In the original code l did this
              Code:
              try &#123; 
                    publisher = getLibrary&#40;&#41;.loadPublisher&#40;RequestUtils.getLongParameter&#40;request,"id",0&#41;&#41;;
                    System.out.println&#40;"+++++++"+publisher+"+++++++"&#41;; 
              &#125;catch&#40;org.springframework.orm.ObjectRetrievalFailureException orfe&#41;&#123; 
                    return new Publisher&#40;&#41;; 
              &#125;
              To summarize , if

              1. lazy="true"
              Code:
              <class name="org.yourschool.library.domain.Publisher" 
                 table="PUBLISHERS" lazy="true">
              2. with comment
              Code:
              System.out.println&#40;"+++++++"+publisher+"+++++++"&#41;;
              3. with OpenSessionInViewFilter

              Result : org.hibernate.ObjectNotFoundException will be throw by H3 (error message in the first post)
              and this is not cause by
              Code:
              publisher = getLibrary&#40;&#41;.loadPublisher&#40;RequestUtils.getLongParameter&#40;request,"id",0&#41;&#41;;
              but
              Code:
              System.out.println&#40;"+++++++"+publisher+"+++++++"&#41;;
              and this is out of bound of spring DAO , so no exception conversion take place , l got a raw Hibernate exception.
              so now exception conversion takes place - hence, you get the raw Hibernate exception
              Is that a mistype ?

              Thanks ..once again .....because it is not easy for me to see the answer, system printout is a foreigner for me , l never thought it can affect the code in PublisherForm.java.

              moon

              Comment

              Working...
              X