用hibernate连续保存一个实体对象时报错

问题描述

高手们:您们好,请教您们个问题,我在用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判断是相同对象个人理解……

时间: 2024-08-02 06:03:12

用hibernate连续保存一个实体对象时报错的相关文章

hibernate可不可以根据一个实体类查询出数据库中是否存在该记录

问题描述 hibernate可不可以根据一个实体类查询出数据库中是否存在该记录 需要判断一条记录是否已经在数据库中是否存在重复记录,用hql的话传入的参数会比较多

android-创建SQLiteDatabase对象时报错,空指针异常

问题描述 创建SQLiteDatabase对象时报错,空指针异常 我创建了一个SQLite,以及操作数据库的类,代码如下(这是数据库类)public class account extends SQLiteOpenHelper { private static final int VERSION = 1;// 定义数据库版本号 private static final String DBNAME = ""account.db"";// 定义数据库名 public a

java tcp通信-JAVA TCP通信,客户端给服务器发送java对象时报错

问题描述 JAVA TCP通信,客户端给服务器发送java对象时报错 我在做JAVA TCP 编程时,我想实现客户端A与客户端B之间通过服务器C来通信,但是我通过客户端A给服务器发送消息时,我传送的是一个java对象MsgPackage类对象,我通过ObjectOutputStream.writeObject来传送,但是服务器去读取这个对象时报错了,没有能正确读取到客户端发送过来的对象,请各位大神指教一下.错误的消息是这样: java.lang.ClassNotFoundException: c

javamail-javaMail 邮件发送成功,但是在保存到已发信箱时报错

问题描述 javaMail 邮件发送成功,但是在保存到已发信箱时报错 1.没有附件时发送完全正常 2.部分附件发送出现如下错误(jpg和pdf格式有误,而word格式正常发送) 错误代码为 folder.appendMessages(message);处 // 保存邮件到指定的收件夹中 public boolean copyMessage(Message[] message, String folderName) throws MessagingException { openFolder(fo

游戏-这个一个bat文件,用来启动一个java项目时报错。大神帮忙解读一下是什么意思。

问题描述 这个一个bat文件,用来启动一个java项目时报错.大神帮忙解读一下是什么意思. 解决方案 解释下你这个$ARGS是用来做什么的. 解决方案二: 目测args是arguments的缩写,代表一个参数,而不是实际的命令.

hibernate+Spring 无法读取实体对象属性

问题描述 实体类代码如下:public class Organization {private int id;private String name;private String sn;private String description;private Organization parent;private Set children;get和set省略}业务实现类://删除机构public void delete(int orgId) {Organization org =(Organizat

sl得到WCF的服务的实体对象提示System.Runtime.Serialization.IExtensibleDataObject没有

问题描述 我在SL中调用WCF服务!其中有一个实体对象如下[DataContract][KnownTypeAttribute(typeof(DataStatisicsForYahooInfo))]publicclassA{[DataMember]publicstringE{get;set;}[DataMember]publicDateTimeD{get;set;}[DataMember]publicDateTimeF{get;set;}}客户端文件中publicpartialclassA:obj

Git.Framework 框架随手记--ORM查询返回实体对象

使用ORM有一个优势,可以通过某种机制将数据库中的数据转化为自己想要的对象形式数据.本章记录一下如何使用Git.Framework返回实体对象   一. Git.Framework 中提供的方法 在Git.Framework中有七个方法可以返回实体对象,先简答的看看这里的方法描述 (1) T GetSingle(int id); (2) T GetSingle(object value); (3) T GetSingle(T entity); (4) V GetSingle<V>(T enti

Hibernate实体对象的生命周期(三种状态详解)

Hibernate生命周期会经历三种不同的状态:     1.Transient(瞬态):实体对象在内存是自由存在的,即与数据库中的数据没有任何关系.换句话说就是:该实体从未与任何持久化上下文关联过,它没有持久化标识.例如:          //创建一个Transient对象  User user=new User(); user.setName("username");  user.setPassword("password");  //此时的user为一个Tr