问题描述
高手们:您们好,请教您们个问题,我在用hibernate连续(一个循环)保存数据,报adifferentobjectwiththesameidentifiervaluewasalreadyassociatedwiththesession我在网上也搜索了解决方法,我也是用了meger,但是不管用,请高手们指点下了哦.org.springframework.orm.hibernate3.HibernateSystemException:adifferentobjectwiththesameidentifiervaluewasalreadyassociatedwiththesession:[com.sinosoft.custommanage.schema.model.PrpDCstUniteHist#0];nestedexceptionisorg.hibernate.NonUniqueObjectException:adifferentobjectwiththesameidentifiervaluewasalreadyassociatedwiththesession:[com.sinosoft.custommanage.schema.model.PrpDCstUniteHist#0]Hibernate:selectcount(*)ascol_0_0_fromprpdcstauditprpdcstaud0_whereprpdcstaud0_.auditstatus='1'and(prpdcstaud0_.comcodein(selectprpdcompan1_.COMCODEfromPRPDCOMPANYprpdcompan1_whereprpdcompan1_.UPPERPATHlike'00000000,43000000%'))atorg.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:676)atorg.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412)atorg.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:424)atorg.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)atorg.springframework.orm.hibernate3.HibernateTemplate.saveOrUpdate(HibernateTemplate.java:748)atins.framework.dao.EntityDaoHibernate.save(EntityDaoHibernate.java:114)atcom.sinosoft.custommanage.service.spring.CustomerUniteHistServiceSpringImpl.save(CustomerUniteHistServiceSpringImpl.java:13)atcom.sinosoft.custommanage.service.spring.CustomerUniteHistServiceSpringImpl$$FastClassByCGLIB$$aa4985ad.invoke(<generated>)atnet.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:149)atorg.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:700)atorg.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)atorg.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106)atorg.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)atorg.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:77)atcom.sinosoft.platform.common.util.LogInterceptor.invoke(LogInterceptor.java:29)atsun.reflect.GeneratedMethodAccessor42.invoke(UnknownSource)atsun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)atjava.lang.reflect.Method.invoke(Method.java:585)atorg.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:627)atorg.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:616)atorg.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:64)atorg.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)atorg.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89)atorg.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)atorg.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:635)atcom.sinosoft.custommanage.service.spring.CustomerUniteHistServiceSpringImpl$$EnhancerByCGLIB$$7dccbc9d.save(<generated>)atcom.sinosoft.custommanage.service.spring.CustomGroupRuleResultServiceSpringImpl.uniteGroup(CustomGroupRuleResultServiceSpringImpl.java:277)atcom.sinosoft.custommanage.service.spring.CustomGroupRuleResultServiceSpringImpl$$FastClassByCGLIB$$2bc6136b.invoke(<generated>)atnet.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:149)atorg.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:700)atorg.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)atorg.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106)atorg.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)atorg.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:77)atcom.sinosoft.platform.common.util.LogInterceptor.invoke(LogInterceptor.java:29)atsun.reflect.GeneratedMethodAccessor42.invoke(UnknownSource)atsun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)atjava.lang.reflect.Method.invoke(Method.java:585)atorg.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:627)atorg.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:616)atorg.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:64)atorg.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)atorg.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89)atorg.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)atorg.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:635)atcom.sinosoft.custommanage.service.spring.CustomGroupRuleResultServiceSpringImpl$$EnhancerByCGLIB$$dc93b47.uniteGroup(<generated>)atcom.sinosoft.custommanage.web.RelationAction.uniteGroup(RelationAction.java:283)atcom.sinosoft.custommanage.web.RelationAction$$FastClassByCGLIB$$10886d44.invoke(<generated>)atnet.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:149)atorg.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:700)atorg.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)atorg.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:77)atcom.sinosoft.platform.common.util.LogInterceptor.invoke(LogInterceptor.java:29)atsun.reflect.GeneratedMethodAccessor42.invoke(UnknownSource)atsun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)atjava.lang.reflect.Method.invoke(Method.java:585)atorg.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:627)atorg.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:616)atorg.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:64)atorg.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)atorg.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89)atorg.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodI
解决方案
解决方案二:
关注一下
解决方案三:
获取到session后调用clear()方法清空session,这是一种解决方法
解决方案四:
连续保存的时候显示关闭SESSION....不让你的缓存中的ID会被应用....
解决方案五:
从你给出的现象来看你要保存的数据不是new出来的新对象。所以你要把对象中的ID(hibernate自动生成的主键)属性设置为null。或使用clone一个对象去更新。
解决方案六:
连续保存需要重新建立session对象
解决方案七:
不同的对象,拥有相同的id,保存对象的时候一般是先把对象放到缓存中,就算是事物提交了也不一定就会及时的清缓存持久化对象,所以你第一次保存一个对象这时它是在缓存中的,然后你又保存了一个对象这时这个对象和上一个对象的oid是一样的,违反了hibernate的规定就会报错,你可以在循环保存对象的时候添加清缓存这个语句。
解决方案八:
清理session。org.springframework.orm.hibernate3.HibernateSystemException:adifferentobjectwiththesameidentifiervaluewasalreadyassociatedwiththesession:说的已经很清楚了,不同对象有相同的主键。。。。
解决方案九:
关注,我也碰到了同样的问题
解决方案十:
知道了,原来DTO里面封装的值出现了乱码?号,有好几个?号,所以hibernate认为它重复了
解决方案十一:
是hashequals问题么?adifferentobjectwiththesameidentifiervaluewasalreadyassociatedwiththesession判断是相同对象个人理解……