异常1:not-null property references a null or transient value
解决方法:将“一对多”关系中的“一”方,not-null设置为false
(参考资料:http://www.thearcmind.com/confluence/pages/viewpage.action?pageId=212)

异常2:org.hibernate.TransientObjectException: object references an unsaved transient instance
解决方法:cascade="save-update,persist"
(参考资料:http://www.laliluna.de/254.html)

异常3:org.hibernate.QueryException: could not resolve property
解决方法:"from Category category where category.userID = :userID"修改为"from Category category whereuserID = :userID"或者"from Category category where category.user.id = :userID"
(参考资料:http://www.laliluna.de/277.html)

异常4:could not initialize proxy - the owning Session was closed
解决方法:设置lazy为false
(参考资料:http://forum.springframework.org/showthread.php?t=27993)

异常2我在应用中碰到了这样的问题:

有三个表:userInfo   deptmentInfo   role

userInfo 与deptmentInfo为many to one

userInfo 与role为many to one

下面为保存UserInfo对象时的代码:

DeptmentInfo dept = new DeptmentInfo();
    dept.setDeptName(deptName);

Role role = new Role();
    role.setRoleName(roleName);

UserInfo user = new UserInfo();
    user.setUserName(userName);
    user.setUserSex(userSex);
    user.setDuty(duty);
    user.setPhone(phone);
    user.setMobileNum(mobileNum);
    user.setEmail(email);
    user.setQq(qq);
    user.setMsn(msn);
    user.setAdress(adress);
    user.setDeptmentInfo(dept);
    user.setRole(role);

dao.save(user);

执行时有错:org.hibernate.TransientObjectException: object references an unsaved transient instance

于是改了UserInfo.hbm.xml的一些地方

如下:

<many-to-one name="role" class="com.oa.domain.Role"
   cascade="save-update,persist" fetch="select">
    <column name="ROLE_ID" precision="22" scale="0" />
   </many-to-one>
   <many-to-one name="deptmentInfo"
    class="com.oa.domain.DeptmentInfo" cascade="save-update,persist"
    fetch="select">
    <column name="DEPT_ID" precision="22" scale="0" />
   </many-to-one>

加了上面红色部分的,就OK了,能保存了。

object references an unsaved transient instance - save the transient instance before flushing错误的更多相关文章

  1. ERROR org.hibernate.internal.SessionImpl - HHH000346: Error during managed flush [object references an unsaved transient instance - save the transient instance before flushing: cn.itcast.domain.Custom

    本片博文整理关于Hibernate中级联策略cascade和它导致的异常: Exception in thread "main" org.hibernate.TransientOb ...

  2. object references an unsaved transient instance save the transient instance before flushing

    object references an unsaved transient instance save the transient instance before flushing 对象引用未保存的 ...

  3. Hibernate的一个问题object references an unsaved transient instance - save the transi5

    1 我做了一对多和多对一的双向关联关系,在User这一方@ManyToOne已经设置了级联Cascade,这样在测试程序中保存User时,Group也应该自动保存,为什么会抛出以下的异常: (我是按着 ...

  4. object references an unsaved transient instance - save the transient instance before flushing: com.jspxcms.core.domain.ScTeam

    object references an unsaved transient instance - save the transient instance before flushing: com.j ...

  5. object references an unsaved transient instance - save the transient instance before flushing异常问题处理

    一.异常:org.hibernate.TransientObjectException: object references an unsaved transient instance - save ...

  6. ManyToMany【项目随笔】关于异常object references an unsaved transient instance

    在保存ManyToMany  时出现异常: org.springframework.dao.InvalidDataAccessApiUsageException: org.hibernate.Tran ...

  7. 三大框架常遇的错误:hibernate : object references an unsaved transient instance

    hibernate : object references an unsaved transient instance 该错误是操作顺序的问题,比如: save或update顺序问题---比方学生表和 ...

  8. object references an unsaved transient instance【异常】

    [异常提示] TransientObjectException: object references an unsaved transient instance -save the transient ...

  9. org.unsaved transient instance - save the transient instance before flushing: bug解决方案

    最近开发和学习过程中,遇到很多零碎的知识点,在此简单地记录下: 1.遇如下bug: org.unsaved transient instance - save the transient instan ...

随机推荐

  1. java代码效率优化

    [转载于http://blog.163.com/user_zhaopeng/blog/static/16602270820122105731329/] 1. 尽量指定类的final修饰符 带有fina ...

  2. [数据库连接池] Java数据库连接池--DBCP浅析.

    前言对于数据库连接池, 想必大家都已经不再陌生, 这里仅仅设计Java中的两个常用数据库连接池: DBCP和C3P0(后续会更新). 一. 为何要使用数据库连接池假设网站一天有很大的访问量,数据库服务 ...

  3. 跨终端 Web

    跨终端 Web(移动优先|响应式|HTML5|Hybrid|桌面+移动应用|一线前端负责人联袂推荐) 徐凯  著   ISBN 978-7-121-23345-6 2014年6月出版 定价:55.00 ...

  4. iOS中的预编译指令的初步探究

    目录 文件包含 #include #include_next #import 宏定义 #define #undef 条件编译 #if #else #endif #if define #ifdef #i ...

  5. iOS-旧项目中手动内存管理(MRC)转ARC

    在ARC之前,iOS内存管理无论对资深级还是菜鸟级开发者来说都是一件很头疼的事.我参 加过几个使用手动内存管理的项目,印象最深刻的是一个地图类应用,由于应用本身就非常耗内存,当时为了解决内存泄露问题, ...

  6. ASP.NET MVC 拦截器IResultFilter

    在ASP.NET MVC中,有一个Result拦截器,实现ResultFilter需要继承一个类(System.Web.Mvc.FilterAttribute)和实现一个类(System.Web.Mv ...

  7. draggable属性设置元素是否可拖动。

    设置标签属性draggable="true"将一个标签内的元素拖动到另外一个标签进行显示: <!DOCTYPE HTML> <html> <head& ...

  8. react8 组件之间的通信

    <body><!-- React 真实 DOM 将会插入到这里 --><div id="example"></div> <!- ...

  9. linux命令 - export - 设置环境变量

    linux命令 - export - 设置环境变量 功能说明:设置或显示环境变量. 语 法:export [-fnp][变量名称]=[变量设置值] 补充说明:在shell中执行程序时,shell会提供 ...

  10. Python中的参数

    Python中的参数 1. python函数参数有多重形式: * test(arg1,arg2,`*args`) * test(arg1,arg2,`*args`,`**kwargs`) 2. 其中比 ...