7 Replies Latest reply on Apr 29, 2008 12:44 PM by yannick bardon

    Problem with em.remove

    yannick bardon Newbie

      Hi,


      I work with JBossSeam-2.0.1.GA and JBoss-4.2.0.GA, my problem is this : when I want to delete with em.remove a record in the database I have this error:



      14:11:13,849 WARN  [arjLoggerI18N] [com.arjuna.ats.arjuna.coordinator.TwoPhaseCo
      ordinator_2] TwoPhaseCoordinator.beforeCompletion - failed for com.arjuna.ats.in
      ternal.jta.resources.arjunacore.SynchronizationImple@841d0f
      javax.persistence.EntityNotFoundException: deleted entity passed to persist: [en
      tityBeans.Document#<null>]
              at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException
      (AbstractEntityManagerImpl.java:613)
              at org.hibernate.ejb.AbstractEntityManagerImpl$1.beforeCompletion(Abstra
      ctEntityManagerImpl.java:524)
              at com.arjuna.ats.internal.jta.resources.arjunacore.SynchronizationImple
      .beforeCompletion(SynchronizationImple.java:114)
              at com.arjuna.ats.arjuna.coordinator.TwoPhaseCoordinator.beforeCompletio
      n(TwoPhaseCoordinator.java:249)
              at com.arjuna.ats.arjuna.coordinator.TwoPhaseCoordinator.end(TwoPhaseCoo
      rdinator.java:88)
              at com.arjuna.ats.arjuna.AtomicAction.commit(AtomicAction.java:177)
              at com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionImple.c
      ommitAndDisassociate(TransactionImple.java:1256)
              at com.arjuna.ats.internal.jta.transaction.arjunacore.BaseTransaction.co
      mmit(BaseTransaction.java:135)
              at com.arjuna.ats.jbossatx.BaseTransactionManagerDelegate.commit(BaseTra
      nsactionManagerDelegate.java:87)
              at org.jboss.tm.usertx.client.ServerVMClientUserTransaction.commit(Serve
      rVMClientUserTransaction.java:140)
              at org.jboss.seam.transaction.UTTransaction.commit(UTTransaction.java:52
      )
              at org.jboss.seam.jsf.SeamPhaseListener.commitOrRollback(SeamPhaseListen
      er.java:592)
              at org.jboss.seam.jsf.SeamPhaseListener.handleTransactionsAfterPhase(Sea
      mPhaseListener.java:330)
              at org.jboss.seam.jsf.SeamPhaseListener.afterServletPhase(SeamPhaseListe
      ner.java:231)
              at org.jboss.seam.jsf.SeamPhaseListener.afterPhase(SeamPhaseListener.jav
      a:182)
              at com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:280)
              at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:117)
      
              at com.icesoft.faces.webapp.http.core.ReceiveSendUpdates.renderCycle(Rec
      eiveSendUpdates.java:57)
              at com.icesoft.faces.webapp.http.core.ReceiveSendUpdates.service(Receive
      SendUpdates.java:45)
              at com.icesoft.faces.webapp.http.core.IDVerifier.service(IDVerifier.java
      :25)
              at com.icesoft.faces.webapp.http.core.ViewBoundServer.service(ViewBoundS
      erver.java:52)
              at com.icesoft.faces.webapp.http.common.standard.PathDispatcherServer$Ma
      tcher.serviceOnMatch(PathDispatcherServer.java:50)
              at com.icesoft.faces.webapp.http.common.standard.PathDispatcherServer.se
      rvice(PathDispatcherServer.java:19)
              at com.icesoft.faces.webapp.http.servlet.ThreadBlockingAdaptingServlet.s
      ervice(ThreadBlockingAdaptingServlet.java:19)
              at com.icesoft.faces.webapp.http.servlet.EnvironmentAdaptingServlet.serv
      ice(EnvironmentAdaptingServlet.java:29)
              at com.icesoft.faces.webapp.http.servlet.MainSessionBoundServlet.service
      (MainSessionBoundServlet.java:109)
              at com.icesoft.faces.webapp.http.servlet.SessionDispatcher.service(Sessi
      onDispatcher.java:35)
              at com.icesoft.faces.webapp.http.servlet.PathDispatcher$Matcher.serviceO
      nMatch(PathDispatcher.java:52)
              at com.icesoft.faces.webapp.http.servlet.PathDispatcher.service(PathDisp
      atcher.java:29)
              at com.icesoft.faces.webapp.http.servlet.MainServlet.service(MainServlet
      .java:98)
              at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
              at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Appl
      icationFilterChain.java:290)
              at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationF
      ilterChain.java:206)
              at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDisp
      atcher.java:687)
              at org.apache.catalina.core.ApplicationDispatcher.processRequest(Applica
      tionDispatcher.java:469)
              at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationD
      ispatcher.java:364)
              at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDis
      patcher.java:301)
              at com.icesoft.faces.webapp.xmlhttp.BlockingServlet.service(BlockingServ
      let.java:54)
              at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Appl
      icationFilterChain.java:290)
              at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationF
      ilterChain.java:206)
              at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter
      .java:83)
              at org.jboss.seam.debug.hot.HotDeployFilter.doFilter(HotDeployFilter.jav
      a:68)
              at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter
      .java:69)
              at org.jboss.seam.web.LoggingFilter.doFilter(LoggingFilter.java:58)
              at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter
      .java:69)
              at org.jboss.seam.web.MultipartFilter.doFilter(MultipartFilter.java:85)
              at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter
      .java:69)
              at org.jboss.seam.web.ExceptionFilter.doFilter(ExceptionFilter.java:64)
              at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter
      .java:69)
              at org.jboss.seam.web.RedirectFilter.doFilter(RedirectFilter.java:45)
              at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter
      .java:69)
              at org.jboss.seam.servlet.SeamFilter.doFilter(SeamFilter.java:158)
              at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Appl
      icationFilterChain.java:235)
              at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationF
      ilterChain.java:206)
              at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFi
      lter.java:96)
              at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Appl
      icationFilterChain.java:235)
              at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationF
      ilterChain.java:206)
              at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperV
      alve.java:230)
              at org.apache.catalina.core.StandardContextValve.invoke(StandardContextV
      alve.java:175)



      From what I have read, you must add a line


      <transaction:ejb-transaction/>




      in components.xml
      But I have the same mistake


      Could you tell me anything can come the problem


      Thank you

        • 1. Re: Problem with em.remove
          Damian Harvey Apprentice

          Show us your code where you call the em.remove(). Normally this error is caused by you removing the object from the entityManager but not removing it from associated lists. For example when removing an OrderLine from an Order:


          em.remove(orderLine);
          order.getOrderLines().remove(orderLine);
          


          Cheers,


          Damian.

          • 2. Re: Problem with em.remove
            yannick bardon Newbie

            Here is my code:


            Class document:



            @Entity
            public class Document implements Serializable{
                 // les variables correspondent aux colonnes de la table Document de la base de données
                 
                 
                 private String nom; // le nom du document
                 
                 
                 private java.util.Date date_creation; // la date de création du document
                 
                 
                 private String emplacement; // l'emplacement du document
                 
                 
                 @OneToMany(cascade={CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE }, fetch = FetchType.LAZY, mappedBy = "document")
                 private java.util.Set<Modification> modification; // la date de dernière modification
                 
                 
                 @ManyToMany(cascade={CascadeType.REMOVE, CascadeType.MERGE, CascadeType.PERSIST}, mappedBy="document", targetEntity=Tag.class,fetch = FetchType.LAZY)
                 private java.util.Set<Tag> tag; // la liste des tags représentant ce document
                 
                 private static final long serialVersionUID = -728268157L; 
                 
                 @Id // tag définissant la clé primaire
                 @GeneratedValue(strategy=GenerationType.AUTO) // tag indiquant que la clé est auto générée
                 private Long id; // l'identifiant obligatoire pour la table
                 
                 
                 @ManyToOne
                 @JoinColumn(name = "typeDocumentID")
                 private TypeDocument typeDocument; // le type du document : ex technique, commercial ..
            
               // les getters et les setters
            





            Class tag:



            @Entity
            public class Tag implements Serializable {
                 //les variables correspondent aux colonnes de la table Tag de la base de données
                 
                 
                 private String nom; // nom du tag
                 
                 
                 private String commentaire; // le commentaire associé
                 
                 
                 @ManyToMany
                 private java.util.Set<Document> document; // un tag peut être associé à plusieurs documents
                 
                 private static final long serialVersionUID = 820497714L;
            
                 @Id // tag définissant la clé primaire
                 @GeneratedValue(strategy=GenerationType.AUTO) // tag indiquant que la clé est auto générée
                 private Long id; // identifiant obligatoire pour la table
            
                  // les getters et les setters
            




            The method is called when you click on the delete button :



            public void supprimer(ActionEvent event){
                      
                             Document document =this.recupereDocument(nomDocumentSelectionner);
                      this.supprimerDocument(document);
                      System.err.println("apres supprimer");
            }
            
            public Document recupereDocument(String nom) {
                      try{
                           Query q = em.createQuery("select d from Document d where d.nom = :nom");  
            
                           q.setParameter("nom",nom);  
            
                           Document d = (Document) q.getSingleResult();  
            
                           return d;  
                      }
                      catch(NoResultException e){
                           e.printStackTrace();
                           return null;
                      }
                 }
            
            
            public void supprimerDocument(Document document) {
                      
                      Query q = em.createQuery("select d from Document d where d.nom = :nom");  
            
                      q.setParameter("nom",document.getNom());  
            
                      Document doc = (Document) q.getSingleResult();
                      
                      doc = em.find(Document.class, document.getId());
                      
                      
                      Set<Tag> tags = this.listeTag(document.getNom());
                      Iterator<Tag> iterator = tags.iterator();
                      while(iterator.hasNext()){
                           
                           Tag t = iterator.next();
                      
                           t = em.find(Tag.class, t.getId());
                           System.err.println("nom du tag = "+t.getNom());
                           doc.getTag().remove(t);
                           t.getDocument().remove(doc);
                      }
                      
                      
                      em.remove(em.find(Document.class, doc.getId()));
                      
                      
                 }
            
            




                 
            thank you for your help

            • 3. Re: Problem with em.remove
              Damian Harvey Apprentice

              What does your page code look like? I assume that you have a list of Documents that you are choosing to delete from?


              It is that list that you need to remove the document from.


              Also, you are doing unnecessary querying of the Database in the supprimerDocument method. You pass the Document in. Then you load the same Document from the database with getSingleResult(). Then you load the same document again using the find().


              Cheers,


              Damian.

              • 4. Re: Problem with em.remove
                yannick bardon Newbie

                I have cleaned my code like this:



                public void supprimerDocument(Document document) {
                          
                          Document doc = em.find(Document.class, document.getId());
                          
                          this.getListeDocument().remove(doc);
                
                          Set<Tag> tags = this.listeTag(document.getNom());
                          Iterator<Tag> iterator = tags.iterator();
                          while(iterator.hasNext()){
                               
                               Tag t = iterator.next();
                          
                               t = em.find(Tag.class, t.getId());
                               System.err.println("nom du tag = "+t.getNom());
                               doc.getTag().remove(t);
                               t.getDocument().remove(doc);
                          }
                          
                          System.err.println("ATTENTION : suppression");
                          em.flush();
                          em.refresh(doc);
                          em.remove(doc);
                          doc=null;
                }
                




                But I always have the same problem


                     
                the goal is to remove a document in the database

                • 5. Re: Problem with em.remove
                  yannick bardon Newbie

                  hi,


                  I solved my problem


                  Thank you

                  • 6. Re: Problem with em.remove
                    Tim Morgan Newbie

                    yannick bardon wrote on Apr 29, 2008 11:35 AM:


                    I solved my problem



                    And how?
                    Sorry to bother you but the solution could be interesting for other people, too.


                    If it is not something very special in your code other people could benefit from your solution when running in a similar problem.


                    Just an idea.

                    • 7. Re: Problem with em.remove
                      yannick bardon Newbie

                      sorry,


                      Here is my solution, against, I do not know why it works better now have.


                      As a reminder, I had this problem, I could not delete a record in the database.
                      In fact in my class java, I had a variable that represented the list of documents which is in the database, and via the method em.remove (doc) I wanted to delete a document, but it did not work, I have therefore made another class in which there are methods to delete a document, in particular em.remove (doc) but in this class, there is not the variable that represents the list of documents.


                      I hope I was clear.


                      Sorry for my English but I am french, and I do not speak English very well


                      Good luck