不能坚持分离的对象

时间:2012-05-05 15:50:00

标签: java postgresql netbeans

我正在使用连接到postresql数据库的java企业应用程序,并在日志文件中出现此错误

javax.ejb.EJBException
at com.sun.ejb.containers.BaseContainer.processSystemException(BaseContainer.java:3894)
at com.sun.ejb.containers.BaseContainer.completeNewTx(BaseContainer.java:3794)
at com.sun.ejb.containers.BaseContainer.postInvokeTx(BaseContainer.java:3596)
at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:1379)
at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:1316)
at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:205)
at com.sun.ejb.containers.EJBLocalObjectInvocationHandlerDelegate.invoke(EJBLocalObjectInvocationHandlerDelegate.java:127)
at $Proxy120.create(Unknown Source)
at com.mirodinero.web.servlets.setMisDatos.processRequest(setMisDatos.java:111)
at com.mirodinero.web.servlets.setMisDatos.doGet(setMisDatos.java:140)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:734)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
at org.apache.catalina.core.ApplicationFilterChain.servletService(ApplicationFilterChain.java:427)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:315)
at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:287)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:218)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:648)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:593)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:94)
at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:98)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:222)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:648)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:593)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:587)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:1096)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:166)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:648)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:593)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:587)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:1096)
at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:288)
at com.sun.enterprise.web.connector.grizzly.DefaultProcessorTask.invokeAdapter(DefaultProcessorTask.java:647)
at com.sun.enterprise.web.connector.grizzly.DefaultProcessorTask.doProcess(DefaultProcessorTask.java:579)
at com.sun.enterprise.web.connector.grizzly.DefaultProcessorTask.process(DefaultProcessorTask.java:831)
at com.sun.enterprise.web.connector.grizzly.DefaultReadTask.executeProcessorTask(DefaultReadTask.java:341)
at com.sun.enterprise.web.connector.grizzly.DefaultReadTask.doTask(DefaultReadTask.java:263)
at com.sun.enterprise.web.connector.grizzly.DefaultReadTask.doTask(DefaultReadTask.java:214)
at com.sun.enterprise.web.connector.grizzly.TaskBase.run(TaskBase.java:265)
at com.sun.enterprise.web.connector.grizzly.ssl.SSLWorkerThread.run(SSLWorkerThread.java:106)

引起:javax.persistence.EntityExistsException: 异常描述:无法持久化分离对象[com.mirodinero.entidad.DatosUsuario [idUsuario = Miguel LLorente]]。 类> com.mirodinero.entidad.DatosUsuario主键> [Miguel LLorente]     at oracle.toplink.essentials.internal.ejb.cmp3.base.EntityManagerImpl.persist(EntityManagerImpl.java:224)     at com.sun.enterprise.util.EntityManagerWrapper.persist(EntityManagerWrapper.java:440)     在com.mirodinero.sesionBean.DatosUsuarioFacade.create(DatosUsuarioFacade.java:30)     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)     在java.lang.reflect.Method.invoke(Method.java:597)     在com.sun.enterprise.security.application.EJBSecurityManager.runMethod(EJBSecurityManager.java:1011)     在com.sun.enterprise.security.SecurityUtil.invoke(SecurityUtil.java:175)     at com.sun.ejb.containers.BaseContainer.invokeTargetBeanMethod(BaseContainer.java:2920)     at com.sun.ejb.containers.BaseContainer.intercept(BaseContainer.java:4011)     at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:197)     ......还有33个 引起:异常[TOPLINK-7231](Oracle TopLink Essentials - 2.1(Build b60e-fcs(12/23/2008))):oracle.toplink.essentials.exceptions.ValidationException 异常描述:无法持久化分离对象[com.mirodinero.entidad.DatosUsuario [idUsuario = Miguel LLorente]]。 类> com.mirodinero.entidad.DatosUsuario主键> [Miguel LLorente]     at oracle.toplink.essentials.exceptions.ValidationException.cannotPersistExistingObject(ValidationException.java:2171)     at oracle.toplink.essentials.internal.sessions.UnitOfWorkImpl.registerNotRegisteredNewObjectForPersist(UnitOfWorkImpl.java:3257)     at oracle.toplink.essentials.internal.ejb.cmp3.base.RepeatableWriteUnitOfWork.registerNotRegisteredNewObjectForPersist(RepeatableWriteUnitOfWork.java:432)     at oracle.toplink.essentials.internal.sessions.UnitOfWorkImpl.registerNewObjectForPersist(UnitOfWorkImpl.java:3226)     at oracle.toplink.essentials.internal.ejb.cmp3.base.EntityManagerImpl.persist(EntityManagerImpl.java:221)     ... 44更多

和此:

javax.persistence.NoResultException: getSingleResult() did not retrieve any entities.
at oracle.toplink.essentials.internal.ejb.cmp3.EJBQueryImpl.throwNoResultException(EJBQueryImpl.java:274)
at oracle.toplink.essentials.internal.ejb.cmp3.base.EJBQueryImpl.getSingleResult(EJBQueryImpl.java:513)
at com.mirodinero.sesionBean.DatosUsuarioFacade.findById(DatosUsuarioFacade.java:57)
at sun.reflect.GeneratedMethodAccessor560.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.sun.enterprise.security.application.EJBSecurityManager.runMethod(EJBSecurityManager.java:1011)
at com.sun.enterprise.security.SecurityUtil.invoke(SecurityUtil.java:175)
at com.sun.ejb.containers.BaseContainer.invokeTargetBeanMethod(BaseContainer.java:2920)
at com.sun.ejb.containers.BaseContainer.intercept(BaseContainer.java:4011)
at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:197)
at com.sun.ejb.containers.EJBLocalObjectInvocationHandlerDelegate.invoke(EJBLocalObjectInvocationHandlerDelegate.java:127)
at $Proxy120.findById(Unknown Source)
at com.mirodinero.web.servlets.setMisDatos.processRequest(setMisDatos.java:96)
at com.mirodinero.web.servlets.setMisDatos.doGet(setMisDatos.java:140)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:734)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
at org.apache.catalina.core.ApplicationFilterChain.servletService(ApplicationFilterChain.java:427)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:315)
at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:287)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:218)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:648)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:593)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:94)
at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:98)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:222)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:648)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:593)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:587)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:1096)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:166)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:648)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:593)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:587)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:1096)
at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:288)
at com.sun.enterprise.web.connector.grizzly.DefaultProcessorTask.invokeAdapter(DefaultProcessorTask.java:647)
at com.sun.enterprise.web.connector.grizzly.DefaultProcessorTask.doProcess(DefaultProcessorTask.java:579)
at com.sun.enterprise.web.connector.grizzly.DefaultProcessorTask.process(DefaultProcessorTask.java:831)
at com.sun.enterprise.web.connector.grizzly.DefaultReadTask.executeProcessorTask(DefaultReadTask.java:341)
at com.sun.enterprise.web.connector.grizzly.DefaultReadTask.doTask(DefaultReadTask.java:263)
at com.sun.enterprise.web.connector.grizzly.DefaultReadTask.doTask(DefaultReadTask.java:214)
at com.sun.enterprise.web.connector.grizzly.TaskBase.run(TaskBase.java:265)
at com.sun.enterprise.web.connector.grizzly.ssl.SSLWorkerThread.run(SSLWorkerThread.java:106)

所以我去检查出现错误的文件,代码如下:

public DatosUsuario findById(String id) {
    DatosUsuario du = null;
    try {
        du = (DatosUsuario) em.createNamedQuery("DatosUsuario.findByIdUsuario").setParameter("idUsuario", id).getSingleResult();
    } catch (Exception nre) {
        log.error("No existe el usuario: " + id + ", ex: ", nre);
    } finally {
        log.debug("du: " + du);
        return du;
    }
}

有人知道错误在哪里吗?因为在我的网页中,它看起来像3次保存数据2。

是的,实际上这里给出了EntityExistExpection:

public void create(DatosUsuario datosUsuario) {
    em.persist(datosUsuario);  // in this line
}

public void edit(DatosUsuario datosUsuario) {
    em.merge(datosUsuario);
}

public void remove(DatosUsuario datosUsuario) {
    em.remove(em.merge(datosUsuario));
}

但我想创建新用户是必要的,对吧?或者它应该检查用户是否存在然后只编辑数据?怎么办呢?

2 个答案:

答案 0 :(得分:1)

可能抛出EntityExistsException,因为您需要执行em.merge()而不是em.persist()。

当实体不是新的并且需要保存时,您应该创建一个由客户调用的save()方法。

public void save(DatosUsuario datosUsuario) {
    em.merge(datosUsuario);
}

或者,您可以保留create()方法并检查实体是否已被持久化。具有@Id和@GeneratedValue(策略= GenerationType.AUTO)的实体仅在持久化时才具有该ID。所以这样的事情可以起作用:

public void createOrSave(DatosUsuario datosUsuario) {
    if (datosUsuario.getId() == null){
        em.persist(datosUsuario);
    }
    else{
        em.merge(datosUsuario);
    }
}

答案 1 :(得分:0)

第一个异常是由一个被分离的对象引起的,这可能有几个原因。就像它是已经结束的交易的一部分,或者它是已关闭的上下文的一部分。如果要在新的事务/上下文中使用它,则需要在实体管理器上调用merge。

您发布的最后一个异常是由于将id传递给findById函数而导致数据库中没有对象。 getSingleResult()的行为是在没有一个结果的情况下抛出异常。