EntityNotFoundException: deleted entity passed to persist

378 views Asked by At

I try to delete one row in my table:

My page:

        <p:dataTable id="table" styleClass="tableHistory" value="#{patientCardMB.history}" var="history" paginator="true" rows="15" rowKey="#{history.id}" selection="#{patientCardMB.patientCard}" selectionMode="single"  >

            <f:facet name="header">
                Historia
            </f:facet>

            <p:column headerText="#{msg.date}">
                <h:outputText id="historyText" value="#{history.dateVisit}"/>
            </p:column>

            <p:ajax event="rowSelect" listener="#{tableBean.onRowSelect()}" update=":form:table" />
            <f:facet name="footer">
                <p:commandButton id="viewButton" value="#{msg.info}" icon="ui-icon-search"  
                                 update=":form:display" oncomplete="userDialog.show()" disabled="#{tableBean.flag}"/>
                <p:commandButton action="#{patientCardMB.createStart()}" value="#{msg.add}" icon="ui-icon-plus" />
                <p:commandButton action="#{patientCardMB.editStart()}" value="#{msg.edit}" disabled="#{tableBean.flag}" ajax="false" icon="ui-icon-pencil"/>
                <p:commandButton value="#{msg.delete}" onclick="confirmation.show()" type="button" disabled="#{tableBean.flag}" icon="ui-icon-close"/>
                <p:confirmDialog id="confirmDialog" message="Jesteś pewien, że chcesz usunąć wybrany wpis?"  
                                 header="Potwierdzenie usunięcia" severity="alert" widgetVar="confirmation"> 
                    <p:commandButton id="confirm" value="Tak" oncomplete="confirmation.hide()"
                                     action="#{patientCardMB.delete()}" />
                    <p:commandButton id="decline" value="Nie" onclick="confirmation.hide()" type="button" /> 
                </p:confirmDialog>
            </f:facet>
        </p:dataTable>

I do a method patientCardMB.delete() :

public String delete() {
    System.out.println(patientCard);
    try {
        patientCardDao.delete(patientCard);
    } catch (EJBException e) {
        sendErrorMessageToUser("Błąd usuwania wpisu");
    }
    sendInfoMessageToUser("Wpis usunięty");
    return "delete";
}

patientCardDAo.delete:

@Override
public void delete(PatientCard patientCard) {
    PatientCard toRemoved = em.merge(patientCard);
    em.remove(toRemoved);
}

Entity PatientCard:

public class PatientCard implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id")
    private Integer id;
    @Basic(optional = false)
    @NotNull
    @Column(name = "dateVisit")
    @Temporal(TemporalType.TIMESTAMP)
    private Date dateVisit;
    @Size(max = 4000)
    @Column(name = "diagnosis")
    private String diagnosis;
    @Size(max = 250)
    @Column(name = "medications")
    private String medications;
    @JoinColumn(name = "patient_id", referencedColumnName = "id")
    @ManyToOne(optional = false)
    private User patientId;
    @JoinColumn(name = "doctor_id", referencedColumnName = "id")
    @ManyToOne(optional = false)
    private User doctorId;

When i try delete i have this error:

WARNING: DTX5014: Caught exception in beforeCompletion() callback: javax.persistence.EntityNotFoundException: deleted entity passed to persist: [pl.ePrzychodnia.model.PatientCard#] at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:621) at org.hibernate.ejb.AbstractEntityManagerImpl$1.beforeCompletion(AbstractEntityManagerImpl.java:532) at com.sun.enterprise.transaction.JavaEETransactionImpl.commit(JavaEETransactionImpl.java:435) at com.sun.enterprise.transaction.JavaEETransactionManagerSimplified.commit(JavaEETransactionManagerSimplified.java:855) at com.sun.ejb.containers.BaseContainer.completeNewTx(BaseContainer.java:5136) at com.sun.ejb.containers.BaseContainer.postInvokeTx(BaseContainer.java:4901) at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:2045) at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:1994) at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:222) at com.sun.ejb.containers.EJBLocalObjectInvocationHandlerDelegate.invoke(EJBLocalObjectInvocationHandlerDelegate.java:89) at com.sun.proxy.$Proxy203.delete(Unknown Source) at pl.ePrzychodnia.mb.PatientCardMB.delete(PatientCardMB.java:102) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:601) at javax.el.BeanELResolver.invokeMethod(BeanELResolver.java:779) at javax.el.BeanELResolver.invoke(BeanELResolver.java:528) at javax.el.CompositeELResolver.invoke(CompositeELResolver.java:257) at com.sun.el.parser.AstValue.invoke(AstValue.java:248) at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:302) at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105) at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88) at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102) at javax.faces.component.UICommand.broadcast(UICommand.java:315) at javax.faces.component.UIData.broadcast(UIData.java:1093) at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794) at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259) at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81) at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118) at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593) at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1550) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175) at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655) at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161) at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231) at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317) at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195) at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:860) at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:757) at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1056) at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:229) at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137) at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104) at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90) at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79) at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54) at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59) at com.sun.grizzly.ContextTask.run(ContextTask.java:71) at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532) at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513) at java.lang.Thread.run(Thread.java:722)

WARNING: EJB5184:A system exception occurred during an invocation on EJB PatientCardDao, method: public void pl.ePrzychodnia.dao.PatientCardDao.delete(pl.ePrzychodnia.model.PatientCard) WARNING: javax.ejb.EJBException: Transaction aborted at com.sun.ejb.containers.BaseContainer.completeNewTx(BaseContainer.java:5142) at com.sun.ejb.containers.BaseContainer.postInvokeTx(BaseContainer.java:4901) at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:2045) at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:1994) at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:222) at com.sun.ejb.containers.EJBLocalObjectInvocationHandlerDelegate.invoke(EJBLocalObjectInvocationHandlerDelegate.java:89) at com.sun.proxy.$Proxy203.delete(Unknown Source) at pl.ePrzychodnia.mb.PatientCardMB.delete(PatientCardMB.java:102) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:601) at javax.el.BeanELResolver.invokeMethod(BeanELResolver.java:779) at javax.el.BeanELResolver.invoke(BeanELResolver.java:528) at javax.el.CompositeELResolver.invoke(CompositeELResolver.java:257) at com.sun.el.parser.AstValue.invoke(AstValue.java:248) at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:302) at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105) at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88) at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102) at javax.faces.component.UICommand.broadcast(UICommand.java:315) at javax.faces.component.UIData.broadcast(UIData.java:1093) at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794) at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259) at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81) at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118) at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593) at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1550) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175) at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655) at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161) at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231) at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317) at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195) at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:860) at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:757) at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1056) at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:229) at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137) at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104) at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90) at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79) at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54) at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59) at com.sun.grizzly.ContextTask.run(ContextTask.java:71) at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532) at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513) at java.lang.Thread.run(Thread.java:722) Caused by: javax.transaction.RollbackException: Transaction marked for rollback. at com.sun.enterprise.transaction.JavaEETransactionImpl.commit(JavaEETransactionImpl.java:473) at com.sun.enterprise.transaction.JavaEETransactionManagerSimplified.commit(JavaEETransactionManagerSimplified.java:855) at com.sun.ejb.containers.BaseContainer.completeNewTx(BaseContainer.java:5136) ... 51 more Caused by: javax.persistence.EntityNotFoundException: deleted entity passed to persist: [pl.ePrzychodnia.model.PatientCard#] at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:621) at org.hibernate.ejb.AbstractEntityManagerImpl$1.beforeCompletion(AbstractEntityManagerImpl.java:532) at com.sun.enterprise.transaction.JavaEETransactionImpl.commit(JavaEETransactionImpl.java:435) ... 53 more

I don't now why i have this error. Earlier I did delete a similar entity, and all work.

1

There are 1 answers

1
Nguyễn Thắng On

I think you're missing process and update attribute in

 <p:commandButton id="confirm" value="Tak" oncomplete="confirmation.hide()"
                                 action="#{patientCardMB.delete()}" process="@parent" update="@form" />