`

[原]Hibernate - TransientObjectException

阅读更多
[标题]:[原]Hibernate - TransientObjectException
[时间]:2009-6-19
[摘要]:object references an unsaved transient instance
[关键字]:Hibernate,
[环境]:Hibernate3.2,MyEclipse7.0,MySQL5.1
[作者]:Winty (wintys@gmail.com) http://www.blogjava.net/wintys

[错误]:
ERROR AbstractFlushingEventListener:301 - Could not synchronize database state with session
org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: wintys.hibernate.manytomany.Course
    at org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:219)
    at org.hibernate.type.EntityType.getIdentifier(EntityType.java:397)
    at org.hibernate.type.ManyToOneType.nullSafeSet(ManyToOneType.java:78)
    at org.hibernate.persister.collection.AbstractCollectionPersister.writeElement(AbstractCollectionPersister.java:755)
    at org.hibernate.persister.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:1143)
    at org.hibernate.action.CollectionRecreateAction.execute(CollectionRecreateAction.java:26)
    at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:250)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:234)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:145)
    at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
    at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
    at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
    at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
    at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
    at wintys.hibernate.manytomany.StudentDAOBean.insert(StudentDAOBean.java:49)
    at org.apache.jsp.index_jsp._jspService(index_jsp.java:68)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:261)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:581)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
    at java.lang.Thread.run(Unknown Source)
object references an unsaved transient instance - save the transient instance before flushing: wintys.hibernate.manytomany.Course

[原因]:
    使用session.save时,相关的级联对象没有被保存。

[解决]:
Either use session.save for both objects or specify a cascade="save-update,persist" in your mapping.
即:
1、加上cascade="all"
<set name="courses" table="student_course" cascade="all">
...
</set>
程序中直接使用。
session.save(student);

2、修改程序,使相关的级联对象先保存。
session.save(course);//加上这一句
session.save(student);//student中关联了course

[参考资料]:
object references an unsaved transient instance - save the transient instance before flushing : http://blog.csdn.net/riseman/archive/2008/03/03/2143661.aspx

原创作品,转载请注明出处。
作者:Winty (wintys@gmail.com)
博客:http://www.blogjava.net/wintys

分享到:
评论

相关推荐

    10_传智播客JPA详解_JPA中的一对多双向关联与级联操作

    在Java世界中,Java Persistence API(JPA)是Oracle公司推出的用于对象关系映射(ORM)的标准框架,它简化了数据库操作,使开发者能够用Java对象来操作数据库记录。本教程将深入探讨JPA中的一对多双向关联及级联...

    hibernate常见错误解决方案

    **TransientObjectException: object references an unsaved transient instance** **异常描述:** 此异常表示一个实体引用了另一个尚未保存的瞬态实体。 **解决方法:** - 在保存实体之前先保存所有关联的瞬态...

    毕业设计-物流管理系统的设计与实现(java)

    3.Caused by:org.hibernate.TransientObjectException:object references an unsaved transient instance 原因:代码中关联的对象没有set值进去。查看代码,发现是enterPriseStrength和enterprise为多对一

    Hibernate映射导致的几个异常

    TransientObjectException: object references an unsaved transient instance 这个异常通常发生在级联操作中,例如当配置了`cascade="save-update,persist"`。如果在保存一个对象时,该对象引用了一个未保存的...

    制作调试过程及数据记录1

    在调试过程中,可能会遇到`Caused by: org.hibernate.TransientObjectException: object references an unsaved transient instance`这样的异常。这个异常通常是因为在持久化操作时,关联的对象没有被正确设置。例如...

    java异常解决方案.docx

    * org.hibernate.TransientObjectException: 对象引用了未保存的瞬态实例 + 解决方案:检查对象是否已经被保存,如果没有,则需要调用 save() 或 update() 方法来保存对象。 * org.springframework.orm.hibernate3....

    java 遇到的各种异常

    `org.hibernate.TransientObjectException`: object references an unsaved transient instance** - **异常描述**:当尝试保存一个对象时,该对象引用了一个尚未被持久化的对象。 - **原因分析**:通常发生在尝试...

    JPA 开发中遇到的错误

    org.hibernate.TransientObjectException: object references an unsaved transient instance 当一个实体引用了另一个尚未保存的实体时,会抛出此类异常。这通常发生在级联保存的场景中,如果父实体尝试保存时,子...

    数据库相关知识

    - 错误处理:当对象状态不匹配或ID设置不正确时,Hibernate会抛出特定异常,如`PersistentObjectException`, `IdentifierGenerationException`, `TransientObjectException`, `NonUniqueObjectException`等。...

Global site tag (gtag.js) - Google Analytics