0 Replies Latest reply on Jan 15, 2013 7:41 AM by Sueleyman Vurucu

    Strange problem with Entitymanager.remove()

    Sueleyman Vurucu Expert

      Hi guys,

       

      I have a strange problem to delete entities in bulk mode from JSF managed CDI bean. If I trigger the delete method from an EJB than it's works as excpected.

       

       

      This is my "Bulk" delete method.

       

       @Override
        public <T extends EditableIF> List<Long> makeTransient(List<T> editables){
          try {
            if (editables == null || editables.size() == 0) {
              log.error("Nothing to delete");
              return new ArrayList<>();
            }
            // Backup the current flushmode
            FlushModeType flushModeBefore = em.getFlushMode();
            em.setFlushMode(FlushModeType.COMMIT);
            int i = 0;
            // Save or update all editables and flush every 50 times
            ArrayList<Long> ids = new ArrayList<>();
            for (T eif : editables) {
              em.remove(mergeIfNeeded(eif));
              ids.add(eif.getId());
              i++;
              if (i % BULK_FLUSH_SIZE == 0) {
                em.flush();
              }
            }
            em.flush();
            em.setFlushMode(flushModeBefore);
            return ids;
          } catch (HibernateException ex) {
            throw new InfrastructureException(ex);
          }
        }
      

       

      The intent of this mehtod is that I wan't improve performance for big deletions. As I said, if I trigger this method from  an EJB then it works fine. But if I trigger this from an SessionScoped CDI bean then the database entries are not deleted.

       

      For tracking the behaviuour of hibernate I switch the loglevel of org.hibernate.event.internal.DefaultDeleteEventListener to TRACE and add a delete interceptor.

       

      The related logentires are shown below:

       

       

      13:36:58,120 TRACE [org.hibernate.event.internal.DefaultDeleteEventListener] (http-localhost-127.0.0.1-8080-4) Deleting a persistent instance
      13:36:58,135 TRACE [org.hibernate.event.internal.DefaultDeleteEventListener] (http-localhost-127.0.0.1-8080-4) Deleting [com.siemag.components.model.message.MessagePrototype#281280]
      13:36:58,135 INFO  [com.siemag.components.server.updating.DomainInterceptor] (http-localhost-127.0.0.1-8080-4) Delete ID 281280 entity com.siemag.components.model.message.MessagePrototype@917079[Id=281280,name=CC011/1444,creationTime=Sun Jan 13 15:09:44 CET 2013]
      13:36:58,135 TRACE [org.hibernate.event.internal.DefaultDeleteEventListener] (http-localhost-127.0.0.1-8080-4) Deleting a persistent instance
      13:36:58,151 TRACE [org.hibernate.event.internal.DefaultDeleteEventListener] (http-localhost-127.0.0.1-8080-4) Deleting [com.siemag.components.model.message.MessagePrototype#281281]
      13:36:58,151 INFO  [com.siemag.components.server.updating.DomainInterceptor] (http-localhost-127.0.0.1-8080-4) Delete ID 281281 entity com.siemag.components.model.message.MessagePrototype@1914f6[Id=281281,name=HBS/145,creationTime=Sun Jan 13 15:09:44 CET 2013]
      13:36:58,634 TRACE [org.hibernate.event.internal.DefaultDeleteEventListener] (http-localhost-127.0.0.1-8080-4) Deleting a persistent instance
      13:36:58,634 TRACE [org.hibernate.event.internal.DefaultDeleteEventListener] (http-localhost-127.0.0.1-8080-4) Deleting [com.siemag.components.model.message.MessagePrototype#281280]
      13:36:58,634 INFO  [com.siemag.components.server.updating.DomainInterceptor] (http-localhost-127.0.0.1-8080-4) Delete ID 281280 entity com.siemag.components.model.message.MessagePrototype@adfa17[Id=281280,name=CC011/1444,creationTime=Sun Jan 13 15:09:44 CET 2013]
      13:36:58,650 TRACE [org.hibernate.event.internal.DefaultDeleteEventListener] (http-localhost-127.0.0.1-8080-4) Deleting a persistent instance
      13:36:58,650 TRACE [org.hibernate.event.internal.DefaultDeleteEventListener] (http-localhost-127.0.0.1-8080-4) Deleting [com.siemag.components.model.message.MessagePrototype#281281]
      13:36:58,650 INFO  [com.siemag.components.server.updating.DomainInterceptor] (http-localhost-127.0.0.1-8080-4) Delete ID 281281 entity com.siemag.components.model.message.MessagePrototype@1616c73[Id=281281,name=HBS/145,creationTime=Sun Jan 13 15:09:44 CET 2013]
      

       

      The strange thing here is that hibernate recognize the delete action but it doesn't excecute a delete statement on the DB.

       

      Thanks in advance.

       

      My runtime:

      Jboss 7.1.1.Final

      Hibernate 4.1.4.Final

      Primefaces 3.4.2