在开发邮箱系统时遇到了这样的问题,
后台报了object references an unsaved transient instance的异常,
我马上检查代码,却找不到问题所在,于是google了一下,搜到了很多结果,
但是所有的答案基本上是映射的问题,我也修改试了,结果是行不通。
应该是别人的解决方案给了我一些提示,最后我解决了这个问题。
异常信息
org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: net.bolue.oa.vo.BlOaMailFolder
at org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:243)
at org.hibernate.type.EntityType.getIdentifier(EntityType.java:456)
at org.hibernate.type.ManyToOneType.isDirty(ManyToOneType.java:264)
at org.hibernate.type.ManyToOneType.isDirty(ManyToOneType.java:275)
at org.hibernate.type.TypeHelper.findDirty(TypeHelper.java:295)
at org.hibernate.persister.entity.AbstractEntityPersister.findDirty(AbstractEntityPersister.java:3403)
at org.hibernate.event.def.DefaultFlushEntityEventListener.dirtyCheck(DefaultFlushEntityEventListener.java:520)
at org.hibernate.event.def.DefaultFlushEntityEventListener.isUpdateNecessary(DefaultFlushEntityEventListener.java:230)
at org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:154)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:219)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:99)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:383)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:133)
at org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:656)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:754)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:723)
at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:393)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:120)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
at $Proxy18.deleteBlOaMailBox(Unknown Source)
at net.bolue.oa.action.BlOaMailAction.absoluteDeleteReceive(BlOaMailAction.java:139)
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.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:269)
at org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:170)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:425)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:228)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:449)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at net.bolue.util.AccessFilter.doFilter(AccessFilter.java:48)
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:88)
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.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:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:859)
at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:579)
at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1555)
at java.lang.Thread.run(Thread.java:619)
BlOaMailBox实体类
private String bombId; //自动编码
private String bomId; //邮件ID
private long userid; //邮件所属用户Id
private Integer bombType; //邮件保存类型
private Long bomfId; //所属文件夹Id
private Integer bombState; //是否垃圾
private Integer bombRead; //是否已读
private BlOaMail bm; //所属邮件
private BlOaMailFolder bmf; //所属文件夹
private BlBasicUser blBasicUser; //所属用户
关系映射
<hibernate-mapping>
<class name="net.bolue.oa.vo.BlOaMailBox" table="bl_oa_mail_box">
<id name="bombId" type="java.lang.String">
<column name="BOMB_ID" length="32" />
<generator class="assigned" />
</id>
<property name="bomId" type="java.lang.String">
<column name="BOM_ID" length="32" not-null="true" />
</property>
<property name="userid" type="java.lang.Long">
<column name="USERID" not-null="true" />
</property>
<property name="bombType" type="java.lang.Integer">
<column name="BOMB_TYPE" not-null="true">
<comment>1为收件,0为发件</comment>
</column>
</property>
<property name="bomfId" type="java.lang.Long">
<column name="BOMF_ID" />
</property>
<property name="bombState" type="java.lang.Integer">
<column name="BOMB_STATE">
<comment>1为是,0为否</comment>
</column>
</property>
<property name="bombRead" type="java.lang.Integer">
<column name="BOMB_READ" />
</property>
<many-to-one class="net.bolue.oa.vo.BlOaMail" column="BOM_ID"
lazy="false" name="bm" outer-join="true" insert="false" update="false"/>
<many-to-one class="net.bolue.oa.vo.BlOaMailFolder" column="BOMF_ID"
lazy="false" name="bmf" outer-join="true" insert="false" update="false"/>
<many-to-one class="net.bolue.basic.vo.BlBasicUser" column="USERID"
lazy="false" name="blBasicUser" outer-join="true" insert="false" update="false"/>
</class>
</hibernate-mapping>
dao实现
Object[] object = (Object[])list.get(0);
blOaMailBox = new BlOaMailBox();
BlOaMail bm = new BlOaMail();
BlOaMailFolder bmf = null;
BlBasicUser bbu = new BlBasicUser();
BlBasicUser sender = new BlBasicUser();
blOaMailBox.setBombId((String)object[0]);
blOaMailBox.setUserid((Long)object[1]);
blOaMailBox.setBombType((Integer)object[2]);
blOaMailBox.setBombState((Integer)object[3]);
blOaMailBox.setBombRead((Integer)object[4]);
bm.setBomId((String)object[5]);
bm.setBomTopic((String)object[6]);
bm.setUserid((Long)object[7]);
bm.setBomRecList((String)object[8]);
bm.setBomCopyList((String)object[9]);
bm.setBomSecList((String)object[10]);
bm.setBomTime((Date)object[11]);
bm.setBomState((Integer)object[12]);
bm.setBomContent((String)object[13]);
if(object[14]!=null){
bmf = new BlOaMailFolder();
bmf.setBomfId((Long)object[14]);
bmf.setBomfName((String)object[15]);
}
blOaMailBox.setBomId(bm.getBomId());
String userIdStr = String.valueOf(blOaMailBox.getUserid());
bbu.setUserId(Integer.parseInt(userIdStr));
bbu.setUserName((String)object[16]);
bbu.setUserAccount((String)object[17]);
String senderIdStr = String.valueOf(bm.getUserid());
sender.setUserId(Integer.parseInt(senderIdStr));
sender.setUserName((String)object[18]);
sender.setUserAccount((String)object[19]);
bm.setSender(sender);
blOaMailBox.setBm(bm);
blOaMailBox.setBmf(bmf);
blOaMailBox.setBlBasicUser(bbu);
最关键的是4和21到25行
如果4行转换为下面的代码就会出错,因为文件夹可以为空,当查找出BlOaMailBox对象
而这个对象里的BlOaMailFolder对象肯定存在,但里面的值却可能为空,在对这个BlOaMailBox对象做一些操作时,比如删除,就会出现上面的异常。
分享到:
相关推荐
级联操作是JPA提供的另一个强大功能,它允许我们在操作一个实体时自动处理与其关联的其他实体。JPA提供了多种级联类型,如`CascadeType.PERSIST`、`CascadeType.MERGE`、`CascadeType.REMOVE`等。例如,如果设置为`...
3.Caused by:org.hibernate.TransientObjectException:object references an unsaved transient instance 原因:代码中关联的对象没有set值进去。查看代码,发现是enterPriseStrength和enterprise为多对一
在调试过程中,可能会遇到`Caused by: org.hibernate.TransientObjectException: object references an unsaved transient instance`这样的异常。这个异常通常是因为在持久化操作时,关联的对象没有被正确设置。例如...
TransientObjectException: object references an unsaved transient instance 这个异常通常发生在级联操作中,例如当配置了`cascade="save-update,persist"`。如果在保存一个对象时,该对象引用了一个未保存的...
org.hibernate.TransientObjectException: object references an unsaved transient instance 当一个实体引用了另一个尚未保存的实体时,会抛出此类异常。这通常发生在级联保存的场景中,如果父实体尝试保存时,子...
**TransientObjectException: object references an unsaved transient instance** **异常描述:** 此异常表示一个实体引用了另一个尚未保存的瞬态实体。 **解决方法:** - 在保存实体之前先保存所有关联的瞬态...
`org.hibernate.TransientObjectException`: object references an unsaved transient instance** - **异常描述**:当尝试保存一个对象时,该对象引用了一个尚未被持久化的对象。 - **原因分析**:通常发生在尝试...
This article describes an introductory object-oriented finite element program for static and dynamic nonlinear applications. This work can be considered as an extension of the original FEM_Object ...
论文提出了一个基于随机递归神经网络(SRNN)的多变量时间序列异常检测框架,能够处理多个相互关联的性能指标,增强系统对复杂异常情况的识别能力。 5. **微软的时间序列异常检测服务** (6)Time-series anomaly ...
20.5 AN OBJECT-ORIENTED ARCHITECTURE 684 20.6 DISCUSSION 693 20.7 BIBLIOGRAPHICAL NOTE 694 Chapter 21: Inheritance case study: “undo” in an interactive system 695 21.1 PERSEVERARE DIABOLICUM 695 ...
references/detection/engine.py , references/detection/utils.py 和 references/ detection/transforms.py
This library defines an object oriented representation of the GitHub API. By "object oriented" we mean there are classes that correspond to the domain model of GitHub (such as GHUser and GHRepository)...
The JVM's garbage collector monitors object references and determines when an object is no longer accessible. Once an object is deemed unreachable, the garbage collector reclaims the associated ...
今天我们要探讨的是一个名为`wagtail_references`的Python库,版本号为0.0.2,它封装了一些特定的功能,以帮助开发者在使用Wagtail CMS(内容管理系统)时更便捷地处理引用数据。 Wagtail CMS是一个强大的、灵活的...
"equals(Object) 与 == 的区别" .equals(Object) 与 == 的区别是 Java 编程语言中一个常见的问题,许多初学者都感到困惑。下面我们将详细地解释这两个概念之间的区别。 首先,让我们看一个例子。假设我们有两个 ...
Python库pelican_references是一个用于处理和管理参考文献的工具,主要面向使用Pelican生成静态博客或网站的开发者。Pelican是Python编写的一款流行的静态站点生成器,它允许用户使用Markdown或reStructuredText格式...
hibernate4.3.5references chm文档
OpenCV(Open Source Computer Vision Library)是一个开源的计算机视觉库,它包含了大量的图像处理和计算机视觉的算法,广泛应用于机器学习、深度学习、图像识别、物体检测等领域。这份“Opencv References”压缩包...
在PyTorch的生态系统中,torch.vision模块提供了图像处理和计算机视觉任务的工具,而其中的references.detection子模块更是专为对象检测任务设计。本篇文章将深入探讨`torch.vision.references.detection`包,以及其...
Feature 1 - Project Cleaner: 1、Actions for unused files 2、Enhanced Project View Feature 2 - Find References: 1、Find references, usages, relationships easily in ...2、Explore usages & references