2 Replies Latest reply on Dec 28, 2006 12:54 PM by lafr

    Remove item from CMR

    dani-cas

      This should be fairly easy, yet I am getting this exception when removing a item from a relation that is working fine.
      I have the following code:

      public boolean borrarProceso(Integer id, String tipo, String userName){
      
       InitialContext ic = null;
       boolean encontro = false;
       AbogadoBeanLocalHome home;
       AdministrativoBeanLocal mover = null;;
       try {
       ic = new InitialContext();
       home = (AbogadoBeanLocalHome) ic.lookup("AbogadoBeanLocal");
       System.out.println("en cambiarAbogadoEnProceso obtengo interface home del old");
       AbogadoBeanLocal abog = home.findByPrimaryKey(userName);
       Collection administrativos = abog.getProcesosAdministrativos();
      
       //Collection administrativosP = new ArrayList();
       //Quiro el proceso que quiero
       Iterator it = administrativos.iterator();
       System.out.println("tengo procesos: "+administrativos.size());
       while(it.hasNext()){
       AdministrativoBeanLocal proc = (AdministrativoBeanLocal) it.next();
       if(id.compareTo(proc.getId())!=0){
       System.out.println("Proceso: "+proc.getId()+" estoy buscando el: "+id);
       //guardo todos con id diff
       //administrativosP.add(proc);
       }else{
      // HERE IS THE IMPORTANT STUFF
       encontro = true;
       //seleccion el unico con id y lo guardo
       mover = (AdministrativoBeanLocal) proc;
       administrativos.remove(mover);
       mover.remove();
      //ENDS HERE
       }
      
       }
       if(encontro){
       System.out.println("Ahora tengo uno menos: "+administrativos.size());
       //Hago set de los nuevos procesos
       abog.setProcesosAdministrativos(administrativos);
       return true;
       }else{
       System.out.println("No encontro el proceso buscado");
       return false;
       }
       }catch (NamingException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
       return false;
       } catch (FinderException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
       return false;
       } catch (EJBException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
       }
       System.out.println("Fallo borrado en centralbean");
       return false;
      
       }
      

      In breaf I search for a bean, and try to remove it, but I cant seem to make it work, sometimes the method works, but when I try to do it agoin the following eception is thrown:
      12:48:49,531 ERROR [STDERR] java.rmi.ServerException: RuntimeException; nested exception is:
       java.lang.IllegalStateException: Underlying collection has been modified
      12:48:49,531 ERROR [STDERR] at org.jboss.ejb.plugins.LogInterceptor.handleException(LogInterceptor.java:421)
      12:48:49,531 ERROR [STDERR] at org.jboss.ejb.plugins.LogInterceptor.invoke(LogInterceptor.java:209)
      12:48:49,531 ERROR [STDERR] at org.jboss.ejb.plugins.ProxyFactoryFinderInterceptor.invoke(ProxyFactoryFinderInterceptor.java:136)
      12:48:49,531 ERROR [STDERR] at org.jboss.ejb.SessionContainer.internalInvoke(SessionContainer.java:648)
      12:48:49,546 ERROR [STDERR] at org.jboss.ejb.Container.invoke(Container.java:954)
      12:48:49,546 ERROR [STDERR] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      12:48:49,546 ERROR [STDERR] at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
      12:48:49,546 ERROR [STDERR] at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
      12:48:49,546 ERROR [STDERR] at java.lang.reflect.Method.invoke(Unknown Source)
      12:48:49,546 ERROR [STDERR] at org.jboss.mx.interceptor.ReflectedDispatcher.invoke(ReflectedDispatcher.java:155)
      12:48:49,546 ERROR [STDERR] at org.jboss.mx.server.Invocation.dispatch(Invocation.java:94)
      12:48:49,546 ERROR [STDERR] at org.jboss.mx.server.Invocation.invoke(Invocation.java:86)
      12:48:49,546 ERROR [STDERR] at org.jboss.mx.server.AbstractMBeanInvoker.invoke(AbstractMBeanInvoker.java:264)
      12:48:49,546 ERROR [STDERR] at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:659)
      12:48:49,546 ERROR [STDERR] at org.jboss.invocation.local.LocalInvoker$MBeanServerAction.invoke(LocalInvoker.java:169)
      12:48:49,546 ERROR [STDERR] at org.jboss.invocation.local.LocalInvoker.invoke(LocalInvoker.java:118)
      12:48:49,546 ERROR [STDERR] at org.jboss.invocation.InvokerInterceptor.invokeLocal(InvokerInterceptor.java:206)
      12:48:49,546 ERROR [STDERR] at org.jboss.invocation.InvokerInterceptor.invoke(InvokerInterceptor.java:192)
      12:48:49,546 ERROR [STDERR] at org.jboss.proxy.TransactionInterceptor.invoke(TransactionInterceptor.java:61)
      12:48:49,546 ERROR [STDERR] at org.jboss.proxy.SecurityInterceptor.invoke(SecurityInterceptor.java:70)
      12:48:49,546 ERROR [STDERR] at org.jboss.proxy.ejb.StatelessSessionInterceptor.invoke(StatelessSessionInterceptor.java:112)
      12:48:49,546 ERROR [STDERR] at org.jboss.proxy.ClientContainer.invoke(ClientContainer.java:100)
      12:48:49,546 ERROR [STDERR] at $Proxy64.borrarProceso(Unknown Source)
      12:48:49,546 ERROR [STDERR] at org.invias.struts.Service.borrarProceso(Service.java:192)
      12:48:49,546 ERROR [STDERR] at org.invias.struts.action.BorrarProcesoDeUsuarioAction.execute(BorrarProcesoDeUsuarioAction.java:40)
      12:48:49,546 ERROR [STDERR] at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)
      12:48:49,546 ERROR [STDERR] at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
      12:48:49,546 ERROR [STDERR] at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
      12:48:49,546 ERROR [STDERR] at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
      12:48:49,546 ERROR [STDERR] at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
      12:48:49,906 ERROR [STDERR] at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
      12:48:49,906 ERROR [STDERR] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
      12:48:49,906 ERROR [STDERR] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
      12:48:49,906 ERROR [STDERR] at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
      12:48:49,906 ERROR [STDERR] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
      12:48:49,906 ERROR [STDERR] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
      12:48:49,906 ERROR [STDERR] at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
      12:48:49,906 ERROR [STDERR] at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
      12:48:49,906 ERROR [STDERR] at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:175)
      12:48:49,906 ERROR [STDERR] at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:74)
      12:48:49,906 ERROR [STDERR] at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
      12:48:49,906 ERROR [STDERR] at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
      12:48:49,906 ERROR [STDERR] at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
      12:48:49,906 ERROR [STDERR] at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
      12:48:49,906 ERROR [STDERR] at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
      12:48:49,906 ERROR [STDERR] at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
      12:48:49,906 ERROR [STDERR] at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
      12:48:49,906 ERROR [STDERR] at org.apache.tomcat.util.net.MasterSlaveWorkerThread.run(MasterSlaveWorkerThread.java:112)
      12:48:49,906 ERROR [STDERR] at java.lang.Thread.run(Unknown Source)
      12:48:49,906 ERROR [STDERR] Caused by: java.lang.IllegalStateException: Underlying collection has been modified
      12:48:49,906 ERROR [STDERR] at org.jboss.ejb.plugins.cmp.jdbc.bridge.RelationSet$1.next(RelationSet.java:404)
      12:48:49,906 ERROR [STDERR] at org.invias.ejb.CentralBean.borrarProceso(CentralBean.java:596)
      12:48:49,906 ERROR [STDERR] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      12:48:49,906 ERROR [STDERR] at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
      12:48:49,906 ERROR [STDERR] at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
      12:48:49,906 ERROR [STDERR] at java.lang.reflect.Method.invoke(Unknown Source)
      12:48:49,906 ERROR [STDERR] at org.jboss.invocation.Invocation.performCall(Invocation.java:359)
      12:48:49,906 ERROR [STDERR] at org.jboss.ejb.StatelessSessionContainer$ContainerInterceptor.invoke(StatelessSessionContainer.java:237)
      12:48:49,906 ERROR [STDERR] at org.jboss.resource.connectionmanager.CachedConnectionInterceptor.invoke(CachedConnectionInterceptor.java:158)
      12:48:49,906 ERROR [STDERR] at org.jboss.ejb.plugins.StatelessSessionInstanceInterceptor.invoke(StatelessSessionInstanceInterceptor.java:169)
      12:48:49,906 ERROR [STDERR] at org.jboss.ws.server.ServiceEndpointInterceptor.invoke(ServiceEndpointInterceptor.java:64)
      12:48:49,906 ERROR [STDERR] at org.jboss.ejb.plugins.CallValidationInterceptor.invoke(CallValidationInterceptor.java:63)
      12:48:49,906 ERROR [STDERR] at org.jboss.ejb.plugins.AbstractTxInterceptor.invokeNext(AbstractTxInterceptor.java:121)
      12:48:49,906 ERROR [STDERR] at org.jboss.ejb.plugins.TxInterceptorCMT.runWithTransactions(TxInterceptorCMT.java:350)
      12:48:49,906 ERROR [STDERR] at org.jboss.ejb.plugins.TxInterceptorCMT.invoke(TxInterceptorCMT.java:181)
      12:48:49,906 ERROR [STDERR] at org.jboss.ejb.plugins.SecurityInterceptor.invoke(SecurityInterceptor.java:168)
      12:48:49,906 ERROR [STDERR] at org.jboss.ejb.plugins.LogInterceptor.invoke(LogInterceptor.java:205)
      12:48:49,906 ERROR [STDERR] ... 47 more
      12:48:49,906 INFO [STDOUT] ha fallado el borrado
      

      I am allmost sure that I am deleting the item wrongly, but I have looked every where for a way to erase beans in a CMR but I havent found much.

      All the help is greatly appreciated.

      Daniel

        • 1. Re: Remove item from CMR
          murtuza52

          The correct way to remove an item from the OneToMany relation is as follows:

          @Entity
          public class A
          {
          private Collection<B> bees;
          @OneToMany (mappedBy="a")
          public Collection<B> getBs()
          {
          return bees;
          }
          public void setBs(Collection<B> b)
          {
          this. bees = b;
          }}
          


          @Entity
          public class B
          {
          private A a;
          @ManyToOne
          public A getA()
          {
          return a;
          }
          public void setContact(A a)
          {
          this.a = a;
          }}
          



          The remove method will look like this in SLSB or SFSB
          
          public void removeAllB(){
           for(B b1: a.getBs()){
           b1.setContact(null);
           }
          manager.merge(a);
          }
          


          Please note in EJB3 the merge method will mark A with null in column for a. This will not remove the record from the database. This must be done using other hibernate specific tags like DELETE_ORPHAN in OneToMany relations or through other techniques.

          Murtuza

          • 2. Re: Remove item from CMR
            lafr

            Calling mover.remove() should be enough.
            The row in the database is deleted and the object is removed from the collection.
            You don't have to remove the object from the collection first.