使用EJB 3.0删除数据库行



我想删除数据库中的一行,所以我创建了一个EJB来为我完成

@Stateless(name = "ejbs/AdminAcountEJB")
public class AdminAcountEJB implements IAdminAcountEJB  {
@PersistenceContext
private EntityManager entityManager;
public void deleteRecord(Record record) {
entityManager.remove(record);
}
}

我没有创建TransactionEntity对象,然后使用begin()方法,因为据我所知,EJB已经是事务安全的。

为了查看它是否在用户界面(JSF 2.0页面)中工作,我创建了一个数据表,其中包含一个带有delete命令链接的列,并从连接到EJB:的backingbean触发它

页面

<h:dataTable value="#{managementBB.retrieveRecords()}" var="record">
...
<h:column>
<h:commandLink value="X" action="#{managementBB.deleteRow(record)}"/>
</h:column>
</h:dataTable>

支持豆

@Named("managementBB")
@SessionScoped
public class ManagementBB implements Serializable{
@EJB
private IAdminAcountEJB managementEJB;
//...
public String deleteRow(Record record) {
managementEJB.deleteRecord(record);
return "manage?faces-redirect=true;";
}

这是我在控制台中看到的错误,当点击链接删除一行时:

警告:StandardWrapperValve[Faces Servlet]:PWC1406:Servlet的Servlet.service()Faces Servlet引发异常javax.faces.el.EvaluationException:位于的javax.ejb.EJBExceptionjavax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressonAdapter.java:102)在com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)位于javax.faces.component.UIDAt.broadcast(UIData.java:912)javax.faces.component.UIWiewRoot.broadcastEvents(UIViewRoot.java:775)在javax.faces.component.UIWiewRoot.processApplication(UIViewRoot.java:1267)在com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:82)在com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)com.sun.faces.lifecycle.Impl.execute(LifecycleImpl.java:118)位于javax.faces.webapp.FacesServlet.service(FacesServlet.java:312)org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1523)在org.apache.catalina.core.StandardWrapperValv.invoke(StandardWrapperValve.java:279)在org.apache.catalina.core.StandardContextValv.invoke(StandardContextValv.java:188)在org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:641)网址:com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:97)com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:85)在org.apache.catalina.core.StandardHostValv.invoke(StandardHostValve.java:185)在org.apache.catalina.connecter.CoyoteAdapter.doService(Coyotedapter.java:325)在org.apache.catalina.connecter.CoyoteAdapter.service(Coyotedapter.java:226)在com.sun.enterprise.v3.services.impl.ContainerMaper.service(ContainerMaper.java:165)在com.sun.grazzy.http.ProcessorTask.invokeAdapter(ProcessorTask.java:791)在com.sun.grazzy.http.ProcessorTask.doProcess(ProcessorTask.java:693)网址:com.sun.grazzly.http.ProcessorTask.produce(ProcessorTask.java:954)在com.sun.grazzy.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:170)在com.sun.grazzy.DefaultProtocolChain.executeProtocolFilter(DefaultProtocol Chain.java:135)在com.sun.grazzy.DefaultProtocolChain.execute(DefaultProtocol Chain.java:102)在com.sun.grazzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:88)在com.sun.grazzy.http.HttpProtocolChain.execute(HttpProtocolChain.java:76)在com.sun.grazzy.ProtocolChainTextTask.doCall(ProtocolchainTextTask.java:53)在com.sun.grazzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:57)在com.sun.grazzy.ContextTask.run(ContextTask.java:69)com.sun.grazzy.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:330)在com.sun.grazzy.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:309)在java.lang.Thread.run(Thread.java:662)由以下原因引起:javax.ejb.EJBException位于com.sun.exb.containers.BaseContainer.processSystemException(BaseContainer.java:5119)在com.sun.exb.containers.BaseContainer.completeNewTx(BaseContainer.java:5017)在com.sun.exb.containers.BaseContainer.postInvokeTx(BaseContainer.java:4805)在com.sun.exb.containers.BaseContainer.postInvoke(BaseContainer.java:2004)在com.sun.exb.containers.BaseContainer.postInvoke(BaseContainer.java:1955)在com.sun.exb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:198)在com.sun.exb.containers.EJBLocalObjectInvocationHandlerDelegate.ioke(EJBLocalObjectInvocationHandler Delegate.java:84)位于$Proxy162.deleteRecord(未知源)backingbeans。位于的ManagementBB.deleteRow(ManagementBB.java:46)位于的sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)位于java.lang.reflect.Method.ioke(Method.java:597)org.jboss.weld.util.reflection.SecureReflections$13.work(SecureReflections.java:304)在org.jboss.weld.util.reflection.SecureReflectionAccess.run(SecureReflectionAccess.java:54)在org.jboss.weld.util.reflection.SecureReflectionAccess.runAsInvocation(SecureReflectionAccess.java:163)在org.jboss.weld.util.reflection.SecureReflections.uinvoke(SecureReflections.java:298)在org.jboss.weld.bean.proxy.ClientProxyMethodHandler.invoke(ClientProxyMethod Handler.java:113)在org.jboss.weld.util.CleanableMethodHandler.invoke(CleanableMethodHandler.java:43)在backingbeans。ManagementBB_$$_javassist_132.deleteRow(ManagementBB_$_javassist _132.java)位于的sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)位于java.lang.reflect.Method.ioke(Method.java:597)javax.el.BeanELResourcer.invokeMethod(BeanELResourcer.java:737)javax.el.BeanELResourcer.invoke(BeanELResourcer.java:467)位于位于com.sun.el.parser.AstValue.ioke(AstValue.java:228)com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:297)在org.jboss.weld.util.el.ForwardingMethodExpression.invoke(ForwardingMethod Expression.java:43)在org.jboss.weld.WeldMethodExpression.invoke(WeldMethod Expression.java:72)在com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:98)在javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressonAdapter.java:88)…还有33个原因:java.lang.IollegalArgumentException:实体必须管理才能调用remove:entities.Record@df8b3d,尝试合并已分离,然后再次尝试删除。在org.eclipse.persistence.internal.sessions。UnitOfWorkImpl.performRemove(UnitOfWorkImpl.java:3539)在org.eclipse.persistence.internal.jpa.EntityManagerImpl.remove(EntityManagerImpl.java:435)在com.sun.enterprise.contacter.common.impl.EntityManagerWrapper.remove(EntityManagerWrapper.java:292)在ejbs。位于的AdminAcountEJB.deleteRecord(AdminAcountEJB.java:23)位于的sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)位于java.lang.reflect.Method.ioke(Method.java:597)org.glassfish.ejb.security.application.EJBSecurityManager.runMethod(EJBSecurityManager.java:1056)在org.glassfish.ejb.security.application.EJBSecurityManager.ioke(EJBSecurityManager.java:1128)在com.sun.exb.containers.BaseContainer.invokeBeanMethod(BaseContainer.java:5292)网址:com.sun.EjbInvocation.invokeBeanMethod(EjbInvocation.java:615)在com.sun.exb.containers.enterceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:797)在com.sun.ejb.EjbInvocation.prough(EjbInvocation.java:567)org.jboss.weld.ejb.SessionBeanInterceptor.aroundInvoke(SessionBeanInterreceptor.java:47)位于sun.reflect.GeneratedMethodAccessor87.invoke(未知源)sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)位于java.lang.reflect.Method.ioke(Method.java:597)com.sun.exb.containers.enterceptors.AroundInvokeInterceptor.entercept(InterceptorManager.java:858)在com.sun.exb.containers.enterceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:797)在com.sun.ejb.EjbInvocation.prough(EjbInvocation.java:567)com.sun.exb.containers.interceptors.SystemInterceptorProxy.doAround(SystemInterceptorProxy.java:157)在com.sun.exb.containers.enterceptors.SystemInterceptorProxy.aroundInvoke(SystemInterceptorProxy.java:139)位于sun.reflect.GeneratedMethodAccessor86.invoke(未知源)sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)位于java.lang.reflect.Method.ioke(Method.java:597)com.sun.exb.containers.enterceptors.AroundInvokeInterceptor.entercept(InterceptorManager.java:858)在com.sun.exb.containers.enterceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:797)在com.sun.exb.containers.enterceptors.InterceptorManager.entercept(InterceptorManager.java:367)在com.sun.exb.containers.BaseContainer.__intercept(BaseContainer.java:5264)在com.sun.exb.containers.BaseContainer.entercept(BaseContainer.java:5252)在com.sun.exb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:190)…还有60个

正如您所看到的,我将CDI用于bean(@Named)而不是@ManagedBean,但我希望这不是原因。此外,我想提到的是,在我的数据库中,我有一个独立的表,它与Record表完全无关,这会影响吗?

答案可以在堆栈本身的行中找到:

由以下原因引起:java.lang.IollegalArgumentException:必须管理实体才能调用remove:entities.Record@df8b3d,尝试合并已分离的,然后再次尝试删除。

EntityManager.remove要求作为参数传入的引用是托管实体,即对象必须存在于持久性上下文中。如果您从JSF管理的bean或CDI管理的bean传入实体引用,那么它仍然是一个非托管/分离的实体,除非您最初是从当前持久性上下文获得实体引用的。

为了进一步理解这一点,您应该了解JPA实体的生命周期。在大多数情况下,从持久性上下文获取的托管实体在事务结束时(即,当持久性上下文结束时)被分离。因此,JSF/CDI托管bean将使用分离的实体,而不是托管实体。当您将从JSF分离的实体传递回EJB时,调用EntityManager.remove将产生所描述的IllegalArgumentException

因此,解决方案是通过使用分离的实体、使用EntityManager.merge(将分离的实体的内容与持久性上下文合并,并返回对托管实体的引用)或EntityManager.find(如果存在,则从持久性上下文返回托管实体)来获得对托管实体的引用。我更喜欢在可能修改分离实体的场景中使用EntityManager.find——当分离实体与其他实体有关系时,如果您在定义必须级联的操作时不小心,那么MERGE可能会级联,而REMOVE操作可能不会级联。

相关内容

  • 没有找到相关文章

最新更新