今天写项目时,在使用hibernate封装的插入方法时,由于需要同时保存多个数据,导致出现identifier of an instance of

如下代码 :(由于最大最小分数不同所以需要插入两条数据)

ExamScoreDivide examScoreDivide=new ExamScoreDivide();
ExamCourse examCourse=new ExamCourse();
examCourse.setExamCourseId(examCourseId); Exam exam=new Exam();
exam.setExamId(examId); examScoreDivide.setDivideId(StringUtil.getUuid());
examScoreDivide.setMinScore(minScore1);
examScoreDivide.setMaxScore(maxScore1);
examScoreDivide.setExamCourse(examCourse);
examScoreDivide.setExam(exam);
examScoreDivide.setCreator(userInfo.getUserName());
examScoreDivide.setCreateTime(DateUtil.currentDate());
examScoreDivide.setDivideName(divideName);
gradExamScoreDivideService.save(examScoreDivide); examScoreDivide.setDivideId(StringUtil.getUuid());
examScoreDivide.setMinScore(minScore2);
examScoreDivide.setMaxScore(maxScore2);
examScoreDivide.setExamCourse(examCourse);
examScoreDivide.setExam(exam);
examScoreDivide.setCreator(userInfo.getUserName());
examScoreDivide.setCreateTime(DateUtil.currentDate());
examScoreDivide.setDivideName(divideName);
gradExamScoreDivideService.save(examScoreDivide);

但结果报该错误,后来知道了原因:

报错的原因是,在没有给对象examScoreDivide 分配初始的内存地址的时候,就进行对象生成,不管生成多少个,都是多个对象的内存指针指向一个内存地址,所以hibernate在存储时就会保存。放在内存里面每次new就等一个新的内存空间,所以不会报错。

解决后的代码:

ExamScoreDivide examScoreDivide=new ExamScoreDivide();
ExamCourse examCourse=new ExamCourse();
examCourse.setExamCourseId(examCourseId); Exam exam=new Exam();
exam.setExamId(examId); examScoreDivide.setDivideId(StringUtil.getUuid());
examScoreDivide.setMinScore(minScore1);
examScoreDivide.setMaxScore(maxScore1);
examScoreDivide.setExamCourse(examCourse);
examScoreDivide.setExam(exam);
examScoreDivide.setCreator(userInfo.getUserName());
examScoreDivide.setCreateTime(DateUtil.currentDate());
examScoreDivide.setDivideName(divideName);
gradExamScoreDivideService.save(examScoreDivide); ExamScoreDivide examScoreDivide1=new ExamScoreDivide();
ExamCourse examCourse1=new ExamCourse();
examCourse1.setExamCourseId(examCourseId); Exam exam1=new Exam();
exam1.setExamId(examId); examScoreDivide1.setDivideId(StringUtil.getUuid());
examScoreDivide1.setMinScore(minScore2);
examScoreDivide1.setMaxScore(maxScore2);
examScoreDivide1.setExamCourse(examCourse1);
examScoreDivide1.setExam(exam1);
examScoreDivide1.setDivideName(divideName);
examScoreDivide1.setCreator(userInfo.getUserName());
examScoreDivide1.setCreateTime(DateUtil.currentDate());
gradExamScoreDivideService.save(examScoreDivide1);

  

Hibernate异常:identifier of an instance of 错误的更多相关文章

  1. 【hibernate】错误:org.hibernate.HibernateException: identifier of an instance of com.agen.entity.Monthdetail was altered from xx to xx

    所报错误: org.hibernate.HibernateException: identifier of an instance of com.agen.entity.Monthdetail was ...

  2. identifier of an instance of xx.entity was altered from xxKey@249e3cb2 to xxKey@74e8f4a3; nested exception is org.hibernate.HibernateException: identifier of an instance of xxentity was altered from错误

    用entityManager保存数据时报错如下 identifier of an instance of xx.entity was altered from xxKey@249e3cb2 to xx ...

  3. 解决org.hibernate.HibernateException: identifier of an instance of com.ahd.entity.Order was altered from2 to 0

    错误信息 严重: Servlet.service() for servlet [springmvc] in context with path [/order] threw exception [Re ...

  4. Caused by: org.hibernate.HibernateException: identifier of an instance of ... is alterde from

    Caused by: org.hibernate.HibernateException: identifier of an instance of ... is alterde from     Hi ...

  5. 161117、使用spring声明式事务抛出 identifier of an instance of

    今天项目组有成员使用spring声明式事务出现下面异常,这里跟大家分享学习下. 异常信息: org.springframework.orm.hibernate3.HibernateSystemExce ...

  6. hibernate 异常

    1.异常:org.hibernate.AnnotationException: No identifier specified for entity异常. entity类是必须要主键的,否则就会报出这 ...

  7. identifier of an instance of com.xxx.model.system.xxxObject was altered from 1765 to 1766

    Caused by: org.hibernate.HibernateException: identifier of an instance of com.xxx.model.system.xxxOb ...

  8. Hibernate 异常总结

    异常一 异常一 异常描述: Sax解析异常:cvc-复杂的类型,发现了以元素maping开头的无效内容,应该是以 ‘{“http://www.hibernate.org/xsd/orm/cfg“:pr ...

  9. spring参数类型异常输出,SpringMvc参数类型转换错误输出

    spring参数类型异常输出, SpringMvc参数类型转换错误输出 >>>>>>>>>>>>>>>> ...

随机推荐

  1. Widows下Faster R-CNN的MATALB配置(CPU)

    目录 1. 准备工作 2. VS2013编译Caffe 3. Faster R-CNN的MATLAB源码测试 说实话,费了很大的劲,在调试的过程中,遇到了很多的问题: 幸运的是,最终还是解决了问题: ...

  2. Vue Study [1]: Vue Setup

    Description The article for vue.js Setup. Original post link:https://www.cnblogs.com/markjiang7m2/p/ ...

  3. Nacos深入浅出(六)

    其实我们发现在我们本地新生成了文件,这个文件就是nacos; 这个文件怎么那么眼熟,不就是我们的controller中的注解里面的参数value么: @Controller @NacosPropert ...

  4. php追加数组的问题

    PHP数组合并两种方法及区别 如果是关联数组,如下: 代码如下: $a = array( 'where' => 'uid=1', 'order' => 'uid', ); $b = arr ...

  5. 双系统安装Linux的步骤以及一些误区

    1.一次安装失败引发的思考 笔者安装双系统(Windows 7+ Ubuntu 16.01)时参考了如何安装win10和linux [ubuntu14]双系统这篇百度经验,却发现安装后并没有如期进入U ...

  6. [TCP/IP]IP协议

    IP数据报 IP是TCP/IP协议族中最核心的协议,所有的TCP.UDP.ICMP.IGMP数据都以IP数据报的格式传输.IP仅提供尽力而为的传输服务,如果发生某种错误,IP会丢失该数据,然后发送IC ...

  7. 1e9个兵临城下(容斥原理)

    链接:https://ac.nowcoder.com/acm/contest/321/A 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言65536 ...

  8. E. Mike and Foam 容斥原理

    http://codeforces.com/problemset/problem/548/E 这题是询问id,如果这个id不在,就插入这个id,然后求a[id1] ,  a[id2]互质的对数. 询问 ...

  9. NPOI导出EXCEL后公式无结果,公式重新计算开关

    但其实这个选项是可以控制的.在NPOI中,这个属性叫做XSSFSheet.ForceFormulaRecalculation. 打开该选项的话,代码为 sheet1.ForceFormulaRecal ...

  10. nginx配置SSL证书实现https服务

    在前面一篇文章中,使用openssl生成了免费证书 后,我们现在使用该证书来实现我们本地node服务的https服务需求.假如我现在node基本架构如下: |----项目 | |--- static ...