1 Reply Latest reply on Dec 16, 2008 3:29 AM by mister pea

    javax.persistence.EntityNotFoundException: deleted entity pa

    mister pea Newbie

      Hi all,

      I have trawled the net for 3 days now and none of the solutions seem to work for me, I hope someone here can help!!!

      I have 2 entity beans:
      Company
      Employee

      Where a company has n employees.

      My Company bean looks something like:

      @Entity
      @Table(name = "company")
      public class Company implements Serializable
      {
       private static final long serialVersionUID = 1L;
      
       @Id
       @Column(name = "company_id",
       nullable = false)
       @GeneratedValue
       private Integer id;
      
       @Column(name = "name",
       nullable = false,
       unique=true)
       private String name;
      
       @OneToMany(cascade = CascadeType.ALL,
       mappedBy = "employee",
       fetch = FetchType.EAGER)
       private Set<Employee> employees;
      


      My Employee bean looks something like:

      @Entity
      @Table(name="employee")
      public class Employee implements Serializable
      {
       private static final long serialVersionUID = 1L;
      
       @Id
       @Column(name = "employee_id",
       nullable = false)
       @GeneratedValue
       private Integer id;
      
       @ManyToOne
       @JoinColumn(name="company_id",
       referencedColumnName="company_id",
       nullable=true)
       private Company company;
      


      Along with the getters, setters, hashCode, equals and toString methods.

      When I try to delete an employee, I get the following error:


      2008-12-16 17:31:06,942 WARN [com.arjuna.ats.arjuna.logging.arjLoggerI18N] [com.arjuna.ats.arjuna.coordinator.TwoPhaseCoordinator_2] TwoPhaseCoordinator.beforeCompletion - failed for com.arjuna.ats.internal.jta.resources.arjunacore.SynchronizationImple@1c67f50
      javax.persistence.EntityNotFoundException: deleted entity passed to persist: [com.pea.myproject.ejb.entities.Employee#]
      at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:613)
      at org.hibernate.ejb.AbstractEntityManagerImpl$1.beforeCompletion(AbstractEntityManagerImpl.java:524)
      at com.arjuna.ats.internal.jta.resources.arjunacore.SynchronizationImple.beforeCompletion(SynchronizationImple.java:114)
      at com.arjuna.ats.arjuna.coordinator.TwoPhaseCoordinator.beforeCompletion(TwoPhaseCoordinator.java:247)
      at com.arjuna.ats.arjuna.coordinator.TwoPhaseCoordinator.end(TwoPhaseCoordinator.java:86)
      at com.arjuna.ats.arjuna.AtomicAction.commit(AtomicAction.java:177)
      at com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionImple.commitAndDisassociate(TransactionImple.java:1389)
      at com.arjuna.ats.internal.jta.transaction.arjunacore.BaseTransaction.commit(BaseTransaction.java:135)
      at com.arjuna.ats.jbossatx.BaseTransactionManagerDelegate.commit(BaseTransactionManagerDelegate.java:87)
      at org.jboss.aspects.tx.TxPolicy.endTransaction(TxPolicy.java:175)
      at org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:87)
      at org.jboss.aspects.tx.TxInterceptor$Required.invoke(TxInterceptor.java:191)
      at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
      at org.jboss.aspects.tx.TxPropagationInterceptor.invoke(TxPropagationInterceptor.java:95)
      at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
      at org.jboss.ejb3.stateless.StatelessInstanceInterceptor.invoke(StatelessInstanceInterceptor.java:62)
      at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
      at org.jboss.aspects.security.AuthenticationInterceptor.invoke(AuthenticationInterceptor.java:77)
      at org.jboss.ejb3.security.Ejb3AuthenticationInterceptor.invoke(Ejb3AuthenticationInterceptor.java:110)
      at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
      at org.jboss.ejb3.ENCPropagationInterceptor.invoke(ENCPropagationInterceptor.java:46)
      at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
      at org.jboss.ejb3.asynchronous.AsynchronousInterceptor.invoke(AsynchronousInterceptor.java:106)
      at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
      at org.jboss.ejb3.stateless.StatelessContainer.localInvoke(StatelessContainer.java:240)
      at org.jboss.ejb3.stateless.StatelessContainer.localInvoke(StatelessContainer.java:210)
      at org.jboss.ejb3.stateless.StatelessLocalProxy.invoke(StatelessLocalProxy.java:84)


      My remove code looks like this:

      @PersistenceContext
      protected transient EntityManager manager;
      
      public void removeEmployee(final Employee employee) {
       Employee empEntity = manager.find(employee.getClass(), employee.getId());
      
       if(empEntity != null) {
       manager.remove(empEntity);
       }
      
      


      Funny thing is the error occurs STRAIGHT AFTER this method gets called (and no further). This doesn't mean the remove(...) was successful because the entry is still there in the database.

      I even print out empEntity.toString() before calling remove(...) to make sure it contains all the values I'm expecting it to contain.

      I'm at a dead end ... I hope someone can shed some light to this issue!

      Thanks!
      pea

        • 1. Re: javax.persistence.EntityNotFoundException: deleted entit
          mister pea Newbie

          My colleague found a solution, but first of all an amendment ...

          In my company bean I had:

           @OneToMany(cascade = CascadeType.ALL,
           mappedBy = "company",
           fetch = FetchType.EAGER)
           private Set<Employee> employees;
          


          not

           @OneToMany(cascade = CascadeType.ALL,
           mappedBy = "employee",
           fetch = FetchType.EAGER)
           private Set<Employee> employees;
          


          but anyway that wasn't problem.
          This is what we did to fix the error we were getting ...

          In the Company bean, changed employees to:

           @OneToMany(cascade = {CascadeType.MERGE, CascadeType.REMOVE, CascadeType.REFRESH},
           mappedBy = "company",
           fetch = FetchType.EAGER)
           private Set<Employee> employees;
          


          So pretty much just removing the CascadeType.PERSIST (which is contained within CascadeType.ALL).

          This works but don't understand why, if someone can explain that'd be awesome.

          (To save myself here ... I did try this at some point but it musn't have worked before due to other code "fixes" I was trying out to fix this problem!)

          There is one little problem to this solution though, if it is a problem to some at all.

          If you have a new Company object containing a set of new Employees, those employees won't get persisted. You won't get an error message or warning message. It just doesn't get persisted.

          Hmm ...

          Hope this helps someone.