12 Replies Latest reply on Oct 11, 2010 4:36 PM by Roberto Requena

    javax.persistence.EntityNotFoundException: deleted entity passed to persist

    W.Alph HAROUNY Newbie

      Hi, I am encountring the following exception:
      'javax.persistence.EntityNotFoundException: deleted entity passed to persist'


      The context of my application is similar to the following:
      1/ I start conversation in a manual mode persistence context
      2/ I load an object
      ...
      3/ I made a remove on the loaded object
      4/ I flush the persistence context (entityManager.flush()), and I have the indicated above exception


      Between Step 2/ and 3/, it happens a lot of things handled by Seam and Jsf:
      - load the object
      - display the object in the first page of the process
      - navigate to the second page of the process


      The object to remove is not modified explicitely by the application code.


      I found an 'awful' bypass:
      3/ I made an entityManager.clear() (I empty the context of persistence by detaching all entities)
      4/ I load the object again using its id
      5/ I made a remove on the freshly loaded object
      6/ I flush the persistence context (entityManager.flush()).


      And it passes, the object is deleted.


      Does anybody have an explanation why I am having an exception in the first scenario ?


      Thx

        • 1. Re: javax.persistence.EntityNotFoundException: deleted entity passed to persist
          Samuel Mendenhall Apprentice

          Do you have a CASCADE.ALL set on a referring collection to the entity in question?  If so, try altering it and removing the PERSIST Cascade.

          • 2. Re: javax.persistence.EntityNotFoundException: deleted entity passed to persist
            W.Alph HAROUNY Newbie

            Hi Sam,


            Yes I have a CASCADE.ALL set on the referring collection to the entity in question.
            I modified to it to PERSIST Cascade and it didn't work.


            Thx

            • 5. Re: javax.persistence.EntityNotFoundException: deleted entity passed to persist
              Val Sw Expert
              Please help..... I am also facing the same issue I tried various options but no success....

              Code:
              -----
              @OneToMany(cascade = {
                      CascadeType.ALL,CascadeType.REMOVE
                  })
                  @JoinTable(name = "EXPERIMENT_REAGENT_J", joinColumns = {
                      @JoinColumn(name = "PARENT_EXPERIMENT_ID")
                  }, inverseJoinColumns = {
                      @JoinColumn(name = "CHILD_REAGENT_ID")
                  })
                  @OrderBy
                  public List<Reagent> getReagent() {
                      if (reagent == null) {
                          reagent = new ArrayList<Reagent>();
                      }
                      return this.reagent;
                  }



              Error :
              javax.persistence.EntityNotFoundException: deleted entity passed to persist.....
              • 6. Re: javax.persistence.EntityNotFoundException: deleted entity passed to persist
                Rob Hills Newbie

                This link from the Hibernate documents explains transitive persistence and CascadeTypes:


                Hibernate CascadeTypes and transitive persistence


                From there, you will see that CascadeType.ALL is equivalent to:


                (cascade = {CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REMOVE, CascadeType.REFRESH})
                


                So, what Samuel meant by removing the PERSIST Cascade is to change from this:


                (cascade = {CascadeType.ALL})
                


                to this:


                  (cascade = {CascadeType.MERGE, CascadeType.REMOVE, CascadeType.REFRESH})
                



                HTH,

                • 7. Re: javax.persistence.EntityNotFoundException: deleted entity passed to persist
                  Val Sw Expert
                  Thanks for the reply Rob...

                  I tried changing it... but still it gives the following error... any help will be appreciated...
                  -----------------------------------------------
                  14:46:24,308 ERROR [ExceptionFilter] exception root cause
                  javax.faces.FacesException: #{quantExperimentHome.remove}: javax.persistence.EntityNotFoundException: deleted entity passed to persist: [com.entity.QuantExperiment#<null>]
                       at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:118)
                  -----------------------------------------------
                  QuantExperiment is the parent entity

                  public class QuantExperiment {..........
                  protected List<Reagent> reagent;
                  ................................
                  @OneToMany(cascade = {
                       CascadeType.PERSIST, CascadeType.REMOVE, CascadeType.REFRESH
                           })
                      @JoinTable(name = "MET_QUANT_EXPERIMENT_REAGENT_J", joinColumns = {
                          @JoinColumn(name = "PARENT_QUANTEXPERIMENT_ID")
                      }, inverseJoinColumns = {
                          @JoinColumn(name = "CHILD_REAGENT_ID")
                      })
                      @OrderBy
                      public List<Reagent> getReagent() {
                          if (reagent == null) {
                              reagent = new ArrayList<Reagent>();
                          }
                          return this.reagent;
                      }

                  public List<Reagent> getReagent() {
                          if (reagent == null) {
                              reagent = new ArrayList<Reagent>();
                          }
                          return this.reagent;
                      }
                  • 8. Re: javax.persistence.EntityNotFoundException: deleted entity passed to persist
                    Stuart Douglas Master

                    Do you have any other classes that reference QuantExperiment?

                    • 9. Re: javax.persistence.EntityNotFoundException: deleted entity passed to persist
                      Val Sw Expert
                      Thanks for looking into it .... pls help here are the details....

                      Yes, this is the hierarchy, root entity is Project then QuantExperiment then Reagent then it goes to lower level....

                      Root entity Project, have OneToMany relation with QuantExperiment and Experiment have @ManyToOne with Project. I am deleting from QuantExperiment (quantExperimentHome.remove()), ideally it should remove all the child elements (reagents, etc...).

                      On the other hand if we delete complete Project (projectHome.remove), it deletes all the realted child elements correctly. But as per requirement there should be an option to delete QuantExperiments...

                      Project.java
                      -------------------------------------------
                      public class Project implements Equals, HashCode, ToString {

                      protected List<QuantExperiment> quantExperiment;
                      ....

                      @OneToMany(cascade = {
                              CascadeType.ALL}, mappedBy="project")
                      @OrderBy
                      public List<QuantExperiment> getQuantExperiment() {
                           if (quantExperiment == null) {
                                quantExperiment = new ArrayList<QuantExperiment>();
                           }
                           return this.quantExperiment;
                      }

                      -------------------------------------------
                      QuantExperiment.java
                      -------------------------------------------

                      QuantExperiment have @OneToMany with reagent
                      public class QuantExperiment implements Equals, HashCode, ToString {
                           protected List<Reagent> reagent;
                           protected Project project;

                      @ManyToOne(optional=false)
                           public Project getProject() {
                                return project;
                           }

                      @OneToMany(cascade = {
                                     CascadeType.PERSIST, CascadeType.REMOVE, CascadeType.REFRESH
                               })
                          @JoinTable(name = "MET_QUANT_EXPERIMENT_REAGENT_J", joinColumns = {
                              @JoinColumn(name = "PARENT_QUANTEXPERIMENT_ID")
                          }, inverseJoinColumns = {
                              @JoinColumn(name = "CHILD_REAGENT_ID")
                          })
                          @OrderBy
                          public List<Reagent> getReagent() {
                              if (reagent == null) {
                                  reagent = new ArrayList<Reagent>();
                              }
                              return this.reagent;
                          }
                      • 10. Re: javax.persistence.EntityNotFoundException: deleted entity passed to persist
                        Val Sw Expert

                        Any help on the above issue would be appreciated...

                        • 11. Re: javax.persistence.EntityNotFoundException: deleted entity passed to persist
                          Thiago F. Crepaldi Newbie

                          Remove CascadeType.PERSIST (or replace CascadeType.ALL by CascadeType.MERGE, CascadeType.REFRESH) on BOTH reference to the collection of the  entities in question.


                          In my example, I have a ManyToMany mapping between TestCase and Transaction classes:


                          TestCase.Java


                          @NotEmpty
                          @ManyToMany(targetEntity = com.example.model.Transaction.class, cascade = { CascadeType.MERGE, CascadeType.REFRESH}, fetch = FetchType.LAZY)
                          @JoinTable(name = "TestCase_Transaction", joinColumns = @JoinColumn(name = "testcase_id", insertable = true, updatable = true), inverseJoinColumns = @JoinColumn(name = "transaction_id", insertable = true, updatable = true))
                          public List<Transaction> getTransactions() {
                             return this.transactions;
                          }
                          



                          Transaction.Java


                          @ManyToMany(fetch = FetchType.EAGER, cascade = { CascadeType.MERGE, CascadeType.REFRESH }, mappedBy = "transactions", targetEntity = com.example.model.TestCase.class)
                          @JoinColumn(insertable = true, updatable = true)
                          @Fetch(value = FetchMode.JOIN)
                          public List<TestCase> getTestcase() {
                             return this.testcase;
                          }

                          • 12. Re: javax.persistence.EntityNotFoundException: deleted entity passed to persist
                            Roberto Requena Newbie

                            Greetings.


                            I am new to the forum, I'm here looking for some help. My problem is this I'm loading a list from the database and when I remove an item I get the following error:


                            16:00:27,901 INFO  [ConfiguracionesIndicadoresUsuariosHome] Id Configuracion = 78 a remover
                            16:00:27,902 INFO  [ConfiguracionesIndicadoresUsuariosHome] Configuracion = ve.gob.mppef.indicadores.entity.ConfiguracionIndicadorUsuario@1cc8124
                            16:00:27,903 FATAL [application] javax.persistence.EntityNotFoundException: deleted entity passed to persist: [ve.gob.mppef.indicadores.entity.ConfiguracionIndicadorUsuario#<null>]
                            javax.faces.el.EvaluationException: javax.persistence.EntityNotFoundException: deleted entity passed to persist: [ve.gob.mppef.indicadores.entity.ConfiguracionIndicadorUsuario#<null>]
                                 at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:91)
                                 at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:91)
                                 at javax.faces.component.UICommand.broadcast(UICommand.java:383)
                                 at org.ajax4jsf.component.AjaxActionComponent.broadcast(AjaxActionComponent.java:55)
                                 at org.ajax4jsf.component.UIDataAdaptor.broadcast(UIDataAdaptor.java:1364)
                                 at org.ajax4jsf.component.AjaxViewRoot.processEvents(AjaxViewRoot.java:324)
                                 at org.ajax4jsf.component.AjaxViewRoot.broadcastEvents(AjaxViewRoot.java:299)



                            The mapping of the class I'm trying to delete it:



                            @Entity
                            @Table(name = "conf_indicadores_x_usuarios")
                            public class ConfiguracionIndicadorUsuario implements Serializable {
                            
                                /* Id para la version de serializacion. */
                                private static final long serialVersionUID = 7305908144694946504L;
                            
                                // Declaracion miembros de clase privados.
                                private Long idConfiguracion;
                            
                                private String accion;
                            
                                private boolean estado;
                            
                                private boolean verValorRef;
                            
                                private Grafico graficoPorDefecto;
                            
                                private Indicador indicador;
                            
                                private String usuario;
                            
                                private RangosVisualizacion rangosVisualizacion;
                            
                                private Dependencia dependencia;
                            
                                private Familia familia;
                            
                                private EjercicioPresupuestario ejercicio;
                            
                                /**
                                 * Constructor por Defecto
                                 */
                                public ConfiguracionIndicadorUsuario() {
                                    super();
                                }
                            
                                /**
                                 * @return the idConfiguracion
                                 */
                                @Id
                                @GeneratedValue(generator = "seq_conf_indicadores_x_usuarios")
                                @SequenceGenerator(name = "seq_conf_indicadores_x_usuarios", sequenceName = "seq_conf_indicadores_x_usuarios")
                                @Column(name = "ID_CONFIGURACION")
                                public Long getIdConfiguracion() {
                                    return idConfiguracion;
                                }
                            
                                /**
                                 * @param idConfiguracion the idConfiguracion to set
                                 */
                                public void setIdConfiguracion(Long idConfiguracion) {
                                    this.idConfiguracion = idConfiguracion;
                                }
                            
                                /**
                                 * @return the accion
                                 */
                                @NotNull
                                @Length(max = 10)
                                @Column(name = "ACCION")
                                public String getAccion() {
                                    return accion;
                                }
                            
                                /**
                                 * @param accion the accion to set
                                 */
                                public void setAccion(String accion) {
                                    this.accion = accion;
                                }
                            
                                /**
                                 * @return the estado
                                 */
                                @Column(name = "ESTADO")
                                public boolean getEstado() {
                                    return estado;
                                }
                            
                                /**
                                 * @param estado the estado to set
                                 */
                                public void setEstado(boolean estado) {
                                    this.estado = estado;
                                }
                            
                                /**
                                 * @return the verValorRef
                                 */
                                @NotNull
                                @Column(name = "VER_VALOR_REF")
                                public boolean getVerValorRef() {
                                    return verValorRef;
                                }
                            
                                /**
                                 * @param verValorRef the verValorRef to set
                                 */
                                public void setVerValorRef(boolean verReferencial) {
                                    this.verValorRef = verReferencial;
                                }
                            
                                /**
                                 * @return the graficoPorDefecto
                                 */
                                @OneToOne
                                @JoinColumn(name = "ID_GRAFICO_DEFECTO")
                                public Grafico getGraficoPorDefecto() {
                                    return graficoPorDefecto;
                                }
                            
                                /**
                                 * @param graficoPorDefecto the graficoPorDefecto to set
                                 */
                                public void setGraficoPorDefecto(Grafico graficoPorDefecto) {
                                    this.graficoPorDefecto = graficoPorDefecto;
                                }
                            
                                /**
                                 * @return the usuario
                                 */
                                @Column(name = "USERS_ID")
                                public String getUsuario() {
                                    return usuario;
                                }
                            
                                /**
                                 * @param usuario the usuario to set
                                 */
                                public void setUsuario(String usuario) {
                                    this.usuario = usuario;
                                }
                            
                                /**
                                 * @return the indicador
                                 */
                                @ManyToOne(fetch = FetchType.LAZY)
                                @JoinColumn(name = "ID_INDICADOR")
                                public Indicador getIndicador() {
                                    return indicador;
                                }
                            
                                /**
                                 * @param indicador the indicador to set
                                 */
                                public void setIndicador(Indicador indicador) {
                                    this.indicador = indicador;
                                }
                            
                                /**
                                 * @return the rangosVisualizacion
                                 */
                                @ManyToOne
                                @JoinColumn(name = "ID_RANGO_VISUALIZACION")
                                public RangosVisualizacion getRangosVisualizacion() {
                                    return rangosVisualizacion;
                                }
                            
                                /**
                                 * @param rangosVisualizacion the rangosVisualizacion to set
                                 */
                                public void setRangosVisualizacion(RangosVisualizacion rangosVisualizacion) {
                                    this.rangosVisualizacion = rangosVisualizacion;
                                }
                            
                                /**
                                 * @return the dependencia
                                 */
                                @ManyToOne
                                @JoinColumn(name = "ID_DEPENDENCIA")
                                public Dependencia getDependencia() {
                                    return dependencia;
                                }
                            
                                /**
                                 * @param dependencia the dependencia to set
                                 */
                                public void setDependencia(Dependencia dependencia) {
                                    this.dependencia = dependencia;
                                }
                            
                                /**
                                 * @return the familia
                                 */
                                @ManyToOne
                                @JoinColumn(name = "ID_FAMILIA")
                                public Familia getFamilia() {
                                    return familia;
                                }
                            
                                /**
                                 * @param familia the familia to set
                                 */
                                public void setFamilia(Familia familia) {
                                    this.familia = familia;
                                }
                            
                                /**
                                 * @return the ejercicio
                                 */
                                @ManyToOne
                                @JoinColumn(name = "ID_EJERCICIO")
                                public EjercicioPresupuestario getEjercicio() {
                                    return ejercicio;
                                }
                            
                                /**
                                 * @param ejercicio the ejercicio to set
                                 */
                                public void setEjercicio(EjercicioPresupuestario ejercicio) {
                                    this.ejercicio = ejercicio;
                                }
                            
                            }




                            I have already several days looking for a solution but I have found it, thanks in advance and excuse my English