当我尝试从数据库中删除数据时:
@Override
public void delete(PatientCard patientCard) {
PatientCard toRemoved = em.merge(patientCard);
em.remove(toRemoved);
}
我收到错误:
警告:EJB5184:调用期间发生系统异常 EJB PatientCardDao,方法:public void pl.ePrzychodnia.dao.daoClass.PatientCardDao.delete(pl.ePrzychodnia.model.PatientCard) 警告:javax.ejb.EJBException at com.sun.ejb.containers.BaseContainer.processSystemException(BaseContainer.java:5215) at com.sun.ejb.containers.BaseContainer.completeNewTx(BaseContainer.java:5113) at com.sun.ejb.containers.BaseContainer.postInvokeTx(BaseContainer.java:4901) 在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) 在com.sun.proxy。$ Proxy275.delete(未知来源) at pl.ePrzychodnia.mb.PatientCardMB.delete(PatientCardMB.java:149) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在java.lang.reflect.Method.invoke(Method.java:601) 在javax.el.BeanELResolver.invokeMethod(BeanELResolver.java:779) 在javax.el.BeanELResolver.invoke(BeanELResolver.java:528) 在javax.el.CompositeELResolver.invoke(CompositeELResolver.java:257) 在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) 在javax.faces.component.UICommand.broadcast(UICommand.java:315) 在javax.faces.component.UIData.broadcast(UIData.java:1093) 在javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794) 在javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259) at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81) 在com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) 在com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118) 在javax.faces.webapp.FacesServlet.service(FacesServlet.java:593) 在org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1550) 在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:343) 在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:217) at pl.ePrzychodnia.filter.FilterLogin.doFilter(FilterLogin.java:49) 在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256) 在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:217) 在org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:279) 在org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175) 在org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655) 在org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595) 在org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161) 在org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331) 在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) 在com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:860) at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:757) 在com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1056) 在com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:229) at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137) 在com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104) 在com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90) 在com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79) at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54) 在com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59) 在com.sun.grizzly.ContextTask.run(ContextTask.java:71) at com.sun.grizzly.util.AbstractThreadPool $ Worker.doWork(AbstractThreadPool.java:532) 在com.sun.grizzly.util.AbstractThreadPool $ Worker.run(AbstractThreadPool.java:513) at java.lang.Thread.run(Thread.java:722)引起:java.lang.IllegalArgumentException:删除分离的实例 pl.ePrzychodnia.model.PatientCard#1 在org.hibernate.ejb.event.EJB3DeleteEventListener.performDetachedEntityDeletionCheck(EJB3DeleteEventListener.java:45) 在org.hibernate.event.def.DefaultDeleteEventListener.onDelete(DefaultDeleteEventListener.java:86) at org.hibernate.event.def.DefaultDeleteEventListener.onDelete(DefaultDeleteEventListener.java:52) 在org.hibernate.impl.SessionImpl.fireDelete(SessionImpl.java:766) 在org.hibernate.impl.SessionImpl.delete(SessionImpl.java:744) 在org.hibernate.ejb.AbstractEntityManagerImpl.remove(AbstractEntityManagerImpl.java:253) at com.sun.enterprise.container.common.impl.EntityManagerWrapper.remove(EntityManagerWrapper.java:303) 在pl.ePrzychodnia.dao.daoClass.PatientCardDao.delete(PatientCardDao.java:48) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在java.lang.reflect.Method.invoke(Method.java:601) 在org.glassfish.ejb.security.application.EJBSecurityManager.runMethod(EJBSecurityManager.java:1052) 在org.glassfish.ejb.security.application.EJBSecurityManager.invoke(EJBSecurityManager.java:1124) at com.sun.ejb.containers.BaseContainer.invokeBeanMethod(BaseContainer.java:5388) at com.sun.ejb.EjbInvocation.invokeBeanMethod(EjbInvocation.java:619) at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:800) 在com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:571) at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.doAround(SystemInterceptorProxy.java:162) at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.aroundInvoke(SystemInterceptorProxy.java:144) at sun.reflect.GeneratedMethodAccessor457.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在java.lang.reflect.Method.invoke(Method.java:601) at com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:861) at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:800) at com.sun.ejb.containers.interceptors.InterceptorManager.intercept(InterceptorManager.java:370) at com.sun.ejb.containers.BaseContainer .__ intercept(BaseContainer.java:5360) at com.sun.ejb.containers.BaseContainer.intercept(BaseContainer.java:5348) at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:214) ...... 52
但是当我改为:
@Override
public void delete(PatientCard patientCard) {
PatientCard toRemoved = em.merge(em.find(PatientCard.class, patientCard.getId()));
em.remove(toRemoved);
}
数据已删除。为什么呢?
我想知道该实体是否受到管理:
toRemoved = em.merge (patientCard)
boolean a = em.contains (toRemoved);
System.out.println (a);
我认为是真的。
为什么?
em.remove(toRemoved);
在一个案例中会产生错误吗?
答案 0 :(得分:2)
分离实体是由任何实体管理器不管理的实体,但代表数据库中的对象。
由于它不是由实体管理器管理,因此无法使用它删除它。因此,您必须先使用实体管理器上的find()
对其进行检索,然后才能由实体管理器进行管理。
因此,find()
就足够了,因为您不会保留更改的属性(即不需要merge()
)。