java - NonUniqueObjectException - 具有相同标识符值的不同对象已与 session 关联

标签 java hibernate

尝试从 session 中删除地址对象时,出现此异常org.hibernate.NonUniqueObjectException:具有相同标识符值的不同对象已与 session 关联

以下是调用源码:

  //The relation between a User and an Address is Ont-To-One with no cascade at any of the joining columns (Address#user and User#address)
    Address address = user.getAddress();
    if(address!=null){
        usr.setAddress(null);    
        //I have tried to merge before/after unbinding the object from the session, neither of both worked
        //addressDao.merge(addressByAddress);
        addressDao.unbindObjectFromSession(addressByAddress);               
        addressDao.remove(addressByAddress);         
    }

AddressDaoImpl.java

public class AddressDaoImpl extends GenericDaoImpl<Address> implements AddressDao {
    private static org.apache.log4j.Logger log = Logger.getLogger(AddressDaoImpl.class);
    public AddressDaoImpl() {
        super(Address.class);
    }

    //omitted source code
}

GenericDaoImpl.java

public class GenericDaoImpl<T extends IdInterface> extends HibernateDaoSupport implements GenericDao<T> {
    private static final Logger LOG = LoggerFactory.getLogger(GenericDaoImpl.class);
    private final Class<T> type;
    public GenericDaoImpl(final Class<T> type) {
        this.type = type;
    }

    @Override
        public boolean remove(final T removeObject) {
        getHibernateTemplate().delete(removeObject);
        LOG.trace("An entity of type \"{}\" has been deleted from db. ID: {}", type.getSimpleName(), removeObject.getId());
        return true;
        }

    @Override

    public void unbindObjectFromSession(final Object unbindObject) {
        getSession().evict(unbindObject);
        getSession().flush();
        LOG.trace("An entity of type \"{}\" has been unbound from session. ID: {}", type.getSimpleName(), unbindObject.getClass().getSimpleName());

        }

    @Override
    public void merge(final Object obj) {
        log.debug("merge("+address.getId()+")");
        this.getHibernateTemplate().merge(obj);                
    }

    //omitted source code
}

异常堆栈跟踪:

SCHWERWIEGEND: org.springframework.orm.hibernate3.HibernateSystemException: a different object with the same identifier value was already associated with the session: [de.fruuts.db.entities.Address#15701]; nested exception is org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [de.fruuts.db.entities.Address#15701]

                at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:676)
                at org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412)
                at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:424)
                at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
                at org.springframework.orm.hibernate3.HibernateTemplate.delete(HibernateTemplate.java:846)
                at org.springframework.orm.hibernate3.HibernateTemplate.delete(HibernateTemplate.java:842)
                at de.fruuts.business.dao.hibernateimpl.GenericDaoImpl.remove(GenericDaoImpl.java:79)
                at sun.reflect.GeneratedMethodAccessor399.invoke(Unknown Source)
                at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
                at java.lang.reflect.Method.invoke(Method.java:597)
                at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:307)
                at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:182)
                at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
                at org.springframework.aop.aspectj.AspectJAfterAdvice.invoke(AspectJAfterAdvice.java:42)
                at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
                at org.springframework.aop.framework.adapter.MethodBeforeAdviceInterceptor.invoke(MethodBeforeAdviceInterceptor.java:50)
                at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
                at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89)
                at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
                at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
                at com.sun.proxy.$Proxy68.remove(Unknown Source)
                at de.fruuts.business.service.impl.UserServiceImpl.removeUsrFruutsPersonalData(UserServiceImpl.java:1731)
                at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
                at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
                at java.lang.reflect.Method.invoke(Method.java:597)
                at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:307)
                at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:182)
                at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
                at org.springframework.aop.aspectj.AspectJAfterAdvice.invoke(AspectJAfterAdvice.java:42)
                at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
                at org.springframework.aop.framework.adapter.MethodBeforeAdviceInterceptor.invoke(MethodBeforeAdviceInterceptor.java:50)
                at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
                at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89)
                at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
                at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
                at com.sun.proxy.$Proxy80.removeUsrFruutsPersonalData(Unknown Source)
                at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
                at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
                at java.lang.reflect.Method.invoke(Method.java:597)
                at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:307)
                at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:182)
                at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
                at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106)
                at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
                at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
                at com.sun.proxy.$Proxy81.removeUsrFruutsPersonalData(Unknown Source)
                at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
                at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
                at java.lang.reflect.Method.invoke(Method.java:597)
                at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:307)
                at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:182)
                at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
                at org.springframework.aop.aspectj.AspectJAfterAdvice.invoke(AspectJAfterAdvice.java:42)
                at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
                at org.springframework.aop.framework.adapter.MethodBeforeAdviceInterceptor.invoke(MethodBeforeAdviceInterceptor.java:50)
                at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
                at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89)
                at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
                at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
                at com.sun.proxy.$Proxy81.removeUsrFruutsPersonalData(Unknown Source)
                at de.fruuts.web.beans.impl.AdminBeanImpl.removeUsrPersonalData(AdminBeanImpl.java:658)
                at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
                at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
                at java.lang.reflect.Method.invoke(Method.java:597)
                at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:307)
                at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:182)
                at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
                at org.springframework.aop.aspectj.AspectJAfterAdvice.invoke(AspectJAfterAdvice.java:42)
                at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
                at org.springframework.aop.framework.adapter.MethodBeforeAdviceInterceptor.invoke(MethodBeforeAdviceInterceptor.java:50)
                at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
                at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89)
                at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
                at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
                at com.sun.proxy.$Proxy150.removeUsrPersonalData(Unknown Source)
                at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
                at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
                at java.lang.reflect.Method.invoke(Method.java:597)
                at org.apache.el.parser.AstValue.invoke(AstValue.java:191)
                at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:276)
                at com.sun.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:68)
                at javax.faces.event.MethodExpressionActionListener.processAction(MethodExpressionActionListener.java:99)
                at javax.faces.event.ActionEvent.processListener(ActionEvent.java:88)
                at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:771)
                at javax.faces.component.UICommand.broadcast(UICommand.java:372)
                at com.icesoft.faces.component.panelseries.UISeries$RowEvent.broadcast(UISeries.java:610)
                at com.icesoft.faces.component.panelseries.UISeries.broadcast(UISeries.java:275)
                at javax.faces.component.UIData.broadcast(UIData.java:938)
                at com.icesoft.faces.component.panelseries.UISeries.broadcast(UISeries.java:270)
                at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:475)
                at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:756)
                at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:82)
                at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:100)
                at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
                at com.icesoft.faces.webapp.http.core.JsfLifecycleExecutor.apply(JsfLifecycleExecutor.java:18)
                at com.icesoft.faces.webapp.http.core.ReceiveSendUpdates.renderCycle(ReceiveSendUpdates.java:122)
                at com.icesoft.faces.webapp.http.core.ReceiveSendUpdates.service(ReceiveSendUpdates.java:73)
                at com.icesoft.faces.webapp.http.core.RequestVerifier.service(RequestVerifier.java:28)
                at com.icesoft.faces.webapp.http.common.standard.PathDispatcherServer.service(PathDispatcherServer.java:24)
                at com.icesoft.faces.webapp.http.servlet.MainSessionBoundServlet.service(MainSessionBoundServlet.java:160)
                at com.icesoft.faces.webapp.http.servlet.SessionDispatcher$1.service(SessionDispatcher.java:42)
                at com.icesoft.faces.webapp.http.servlet.ThreadBlockingAdaptingServlet.service(ThreadBlockingAdaptingServlet.java:19)
                at com.icesoft.faces.webapp.http.servlet.EnvironmentAdaptingServlet.service(EnvironmentAdaptingServlet.java:63)
                at com.icesoft.faces.webapp.http.servlet.SessionDispatcher.service(SessionDispatcher.java:62)
                at com.icesoft.faces.webapp.http.servlet.SessionVerifier.service(SessionVerifier.java:22)
                at com.icesoft.faces.webapp.http.servlet.PathDispatcher.service(PathDispatcher.java:23)
                at com.icesoft.faces.webapp.http.servlet.MainServlet.service(MainServlet.java:153)
                at javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
                at com.icesoft.faces.webapp.xmlhttp.BlockingServlet.service(BlockingServlet.java:56)
                at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
                at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
                at com.ocpsoft.pretty.PrettyFilter.doFilter(PrettyFilter.java:71)
                at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
                at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
                at org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:198)
                at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
                at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
                at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
                at org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:378)
                at org.springframework.security.intercept.web.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:109)
                at org.springframework.security.intercept.web.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:83)
                at org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:390)
                at org.springframework.security.ui.SessionFixationProtectionFilter.doFilterHttp(SessionFixationProtectionFilter.java:67)
                at org.springframework.security.ui.SpringSecurityFilter.doFilter(SpringSecurityFilter.java:53)
                at org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:390)
                at org.springframework.security.ui.ExceptionTranslationFilter.doFilterHttp(ExceptionTranslationFilter.java:101)
                at org.springframework.security.ui.SpringSecurityFilter.doFilter(SpringSecurityFilter.java:53)
                at org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:390)
                at org.springframework.security.wrapper.SecurityContextHolderAwareRequestFilter.doFilterHttp(SecurityContextHolderAwareRequestFilter.java:91)
                at org.springframework.security.ui.SpringSecurityFilter.doFilter(SpringSecurityFilter.java:53)
                at org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:390)
                at org.springframework.security.ui.AbstractProcessingFilter.doFilterHttp(AbstractProcessingFilter.java:277)
                at org.springframework.security.ui.SpringSecurityFilter.doFilter(SpringSecurityFilter.java:53)
                at org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:390)
                at org.springframework.security.ui.logout.LogoutFilter.doFilterHttp(LogoutFilter.java:89)
                at org.springframework.security.ui.SpringSecurityFilter.doFilter(SpringSecurityFilter.java:53)
                at org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:390)
                at org.springframework.security.context.HttpSessionContextIntegrationFilter.doFilterHttp(HttpSessionContextIntegrationFilter.java:235)
                at org.springframework.security.ui.SpringSecurityFilter.doFilter(SpringSecurityFilter.java:53)
                at org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:390)
                at org.springframework.security.util.FilterChainProxy.doFilter(FilterChainProxy.java:175)
                at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:236)
                at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:167)
                at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
                at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
                at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:96)
                at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
                at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
                at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
                at de.fruuts.web.filter.Log4jSessionFilter.doFilter(Log4jSessionFilter.java:49)
                at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
                at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
                at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
                at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
                at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
                at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
                at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
                at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
                at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)
                at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606)
                at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
                at java.lang.Thread.run(Thread.java:695)
Caused by: org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [de.fruuts.db.entities.Address#15701]
                at org.hibernate.engine.StatefulPersistenceContext.checkUniqueness(StatefulPersistenceContext.java:556)
                at org.hibernate.event.def.DefaultDeleteEventListener.onDelete(DefaultDeleteEventListener.java:88)
                at org.hibernate.event.def.DefaultDeleteEventListener.onDelete(DefaultDeleteEventListener.java:49)
                at org.hibernate.impl.SessionImpl.fireDelete(SessionImpl.java:766)
                at org.hibernate.impl.SessionImpl.delete(SessionImpl.java:744)
                at org.springframework.orm.hibernate3.HibernateTemplate$25.doInHibernate(HibernateTemplate.java:852)                   at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:419)
                ... 164 more

最佳答案

您想要完成的工作应该像这样:

// Start a spring transaction that wraps the operation
@Transactional
public void removeAddressById(Long addressId) {
  // I'm just locating the address from the database based on some provided id.
  final Address address = addressDao.findById( addressId );
  // break the association between the associated User and Address.
  address.getUser().setAddress( null );
  address.setUser( null );
  // Update the user and remove the address
  // If address owns the one-to-one association, then the merge is unnecessary
  getHibernateTemplate().merge( user );
  getHibernateTemplate().delete( address );
}

由于您尝试删除/移除实体,因此持久性提供程序将自动从持久性上下文中逐出该对象。

关于java - NonUniqueObjectException - 具有相同标识符值的不同对象已与 session 关联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52480166/

相关文章:

java - JButtons 鼠标事件

java - 带有嵌入式 HTML 的 PDF 报告

java - 将属性写入内存缓存,但不写入 Objectify 中的数据存储?

java - 尽管所需的索引是 'serving' ,但为什么我的 AppEngine 查询会抛出 DatastoreNeedIndexExceptions ?

java - struts2 + hibernate3 + jasper-report 5.0 ----- 得到空白报告

java - 找到对集合 org.hibernate.HibernateException 的共享引用

java - 如何使用Spring事务管理将当前用户更新到数据库?

java - JSF 中 "Back"导航链接的最佳实践

hibernate - 何时使用 EntityManager.clear()?

java - 在前端编程线程对话的正确方法