Hibernate:使用重复对象标识符进行异常处理
错误消息“Hibernate:具有相同标识符值的不同对象已与当 Hibernate 遇到具有已与当前会话关联的现有标识符的对象时,就会发生“session”。当尝试保存或更新会话中已存在的对象时,可能会出现这种情况,从而导致冲突。
在提供的代码中:
<code class="java">public E save(E e) { Session session = null; try { session = sessionFactory.openSession(); log.debug("session="+session.hashCode()+" save "+e); session.saveOrUpdate(e); //here throws exception session.flush(); } catch (Exception e1) { log.err("Cannot open hibernate session "+ e1.getMessage()+" cause : "+e1.getCause()); e1.printStackTrace(); } finally { if ( session != null ) session.close(); session = null;} return e ; }</code>
尝试时抛出异常保存或更新用户 bean。问题的原因似乎是用户 bean 包含组 bean 列表,而组 bean 又包含角色列表。保存用户 bean 时,Hibernate 会尝试保存角色两次:一次作为组 bean 的一部分,另一次作为用户 bean 的角色列表的一部分。
userbean#1 |---|-----------***userbean.groups | | groupbean#1 | | groupbean.roles | | rolebean#1 # save relebean#1 the first time | | ---done rolebean#1 | | ------done all rolebeans of group.roles | | ---done groupbean#1 | |-----------done all groupbeans of userbean.groups |---|-----------***userbean.roles | rolebean#1 # save rolebean#1 the second time, and throws exception here! | ----done rolebean#1 | ..... |-----------done all rolebeans of userbean.roles
要解决此问题,请考虑以下方法:
- 使用 session.merge() 而不是 session.saveOrUpdate()**:这将告诉 Hibernate 该对象已经被管理并且应该如有必要,请更新,而不是重新保存。
- 确保在保存或更新之前将对象附加到会话:这可以使用 session.load() 或 session 来实现.get() 从数据库中检索对象。
以上是为什么 Hibernate 在保存具有嵌套集合的用户 bean 时会抛出'具有相同标识符值的不同对象已与会话关联”异常,以及如何解决的详细内容。更多信息请关注PHP中文网其他相关文章!

Java在不同操作系统上的表现存在细微差异。1)JVM实现不同,如HotSpot、OpenJDK,影响性能和垃圾回收。2)文件系统结构和路径分隔符不同,需使用Java标准库处理。3)网络协议实现差异影响网络性能。4)GUI组件外观和行为在不同系统上有别。通过使用标准库和虚拟机测试,可减少这些差异的影响,确保Java程序稳定运行。

javaoffersrobustobject-IentiendedProgrammming(OOP)和Top-Notchsecurityfeatures.1)OopinjavainCludesClasses,对象,继承,多态性,和列出,andeclingfleximaintainablesys.ss.2)SecurityFeateTuersLudEtersludEterMachine(

JavaScriptandJavahavedistinctstrengths:JavaScriptexcelsindynamictypingandasynchronousprogramming,whileJavaisrobustwithstrongOOPandtyping.1)JavaScript'sdynamicnatureallowsforrapiddevelopmentandprototyping,withasync/awaitfornon-blockingI/O.2)Java'sOOPf

JAVAACHIEVESPLATFORMINDEPENTENCETHROUGHJAVAVIRTAILMACHINE(JVM)和BYTECODE.1)THEJVMINTERPRETSBBYTECODE,允许theingthesmecodetorunonanyanyanyanyplatformwithajvm.2)

Java'splatFormIndependecemeanSapplicationsCanrunonAnyPlatFormWithAjvm,使“ Writeonce,RunanyWhere”。

JVM'SperformanceIsCompetitiveWithOtherRuntimes,operingabalanceOfspeed,安全性和生产性。1)JVMUSESJITCOMPILATIONFORDYNAMICOPTIMIZAIZATIONS.2)c提供NativePernativePerformanceButlanceButlactsjvm'ssafetyFeatures.3)

JavaachievesPlatFormIndependencEthroughTheJavavIrtualMachine(JVM),允许CodeTorunonAnyPlatFormWithAjvm.1)codeisscompiledIntobytecode,notmachine-specificodificcode.2)bytecodeisisteredbytheybytheybytheybythejvm,enablingcross-platerssectectectectectross-eenablingcrossectectectectectection.2)

TheJVMisanabstractcomputingmachinecrucialforrunningJavaprogramsduetoitsplatform-independentarchitecture.Itincludes:1)ClassLoaderforloadingclasses,2)RuntimeDataAreafordatastorage,3)ExecutionEnginewithInterpreter,JITCompiler,andGarbageCollectorforbytec


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

安全考试浏览器
Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

WebStorm Mac版
好用的JavaScript开发工具

Dreamweaver CS6
视觉化网页开发工具

记事本++7.3.1
好用且免费的代码编辑器

螳螂BT
Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。