users@glassfish.java.net

javax.persistence.EntityNotFoundException: deleted entity passed to persist

From: <glassfish_at_javadesktop.org>
Date: Thu, 22 Jan 2009 13:23:29 PST

Please help I tried various option but not able to figure out... :( (not an expert...)

I am trying to delete entity QuantExperiment by calling "quantExperimentHome.remove()" it gives the below error : plsssss suggest...

public class QuantExperimentHome extends EntityHome<QuantExperiment>{
        public String remove(){
         super.getInstance().getProject().getQuantExperiment().remove(getInstance());
         return super.remove();
        }
}

Error:
----------------------
ERROR [application] [b]javax.persistence.EntityNotFoundException: deleted entity passed to persist: [com.entity.QuantExperimentHistoryRecords#<null>][/b]javax.faces.el.EvaluationException: javax.persistence.EntityNotFoundException: deleted entity passed to persist: [com.entity.QuantExperimentHistoryRecords#<null>]
 at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102)

Details:
--------
a) There are 3 entities Project, QuantExperiment and QuantExperimentHistoryRecords, please refer below.

b) "super.getInstance().getProject().getQuantExperiment().remove(getInstance());" is used to delete a QuantExperiment, first we need to remove the reference of child from parent before calling "quantExperimentHome.remove()" that is why it is used otherwise it will give the error as "javax.persistence.EntityNotFoundException: deleted entity passed to persist: [com.entity.QuantExperiment#<null>".

c) We added the QuantExperimentHistoryRecords later to track the history of QuantExperiments, information is stored correctly.

Project.java entity
-------------------
public class Project implements Equals, HashCode, ToString {
 protected List<QuantExperiment> quantExperiment;
 protected List<QuantExperimentHistoryRecords> quantExperimentHistoryRecords;
   @OneToMany(cascade = {CascadeType.ALL}, fetch = FetchType.LAZY, mappedBy="project")
    public List<QuantExperimentHistoryRecords> getQuantExperimentHistoryRecords() {
        if (quantExperimentHistoryRecords == null) {
         quantExperimentHistoryRecords = new ArrayList<QuantExperimentHistoryRecords>();
        }
        return this.quantExperimentHistoryRecords;
    }
    setter....

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

QuantExperiment.java entity
---------------------------
public class QuantExperiment implements Equals, HashCode, ToString {
protected Project project;
protected List<QuantExperimentHistoryRecords> quantExperimentHistoryRecords;

@ManyToOne(fetch = FetchType.LAZY, optional=false)
public Project getProject() {
 return project;
} setter....

@OneToMany(cascade = {CascadeType.ALL}, fetch = FetchType.LAZY, mappedBy="quantExperiment")
public List<QuantExperimentHistoryRecords> getQuantExperimentHistoryRecords() {
 if (quantExperimentHistoryRecords == null) {
  quantExperimentHistoryRecords = new ArrayList<QuantExperimentHistoryRecords>();
 }
 return this.quantExperimentHistoryRecords;
} setter....

QuantExperimentHistoryRecords.java entity
-----------------------------------------
public class QuantExperimentHistoryRecords implements Equals, HashCode, ToString {
 protected Project project;
 protected QuantExperiment quantExperiment;

 @ManyToOne(fetch = FetchType.LAZY, optional=false)
    public Project getProject() {
        return project;
    }... setter...

 @ManyToOne(fetch = FetchType.LAZY, optional=false)
    public QuantExperiment getQuantExperiment() {
        return quantExperiment;
    }... setter...
[Message sent by forum member 'salv' (salv)]

http://forums.java.net/jive/thread.jspa?messageID=327720