今天在使用一对多,多对一保存数据的时候出现了这个错误

Hibernate错误:


Exception in thread "main" org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: zhongfucheng.domain.Employee

为啥出现这个错误呢???在保存对象的时候,发现这个对象还有存在外键,这个外键是在另一个实体里面。这个实体如果不存在或者实体的主键为空,就会报这个错误!

而我在保存的时候,并没有把拥有外键的对象保存进去….只保存了“一”方面的对象。


//创建对象
Dept dept = new Dept();
dept.setDeptName("开发部"); Employee zs = new Employee();
zs.setEmpName("张珊");
zs.setSalary(1111);
Employee ls = new Employee();
ls.setEmpName("李四");
ls.setSalary(2222); //添加关系
dept.getSet().add(zs);
dept.getSet().add(ls);
session.save(dept); /*当时我没有把Emploey对象保存一起保存...因此出现了这个错误
session.save(zs);
session.save(ls);*/

还有一个解决方案:在映射文件中配置级联保存更新

Exception in thread "main" org.hibernate.TransientObjectException: object references an unsaved tran的更多相关文章

  1. 在运行Hibernate Hello World程序的时候,抛如下错误: view plain Exception in thread "main" org.hibernate.exception.LockAcquisitionException 解决方法

    在运行Hibernate Hello World程序的时候,抛如下错误: Exception in thread "main" org.hibernate.exception.Lo ...

  2. Exception in thread "main" org.hibernate.HibernateException: save is not valid without active transaction

    在spring4+hibernate4整合过程中,使用@Transactional注解事务会报"Exception in thread "main" org.hibern ...

  3. Hibernate错误:Exception in thread "main" org.hibernate.exception.SQLGrammarException: Could not execute JDBC batch update

    报错:Exception in thread "main" org.hibernate.exception.SQLGrammarException: Could not execu ...

  4. Exception in thread "main" org.hibernate.MappingException: You may only specify a cache for root

    如果出现类似下面的错误: Exception in thread "main" org.hibernate.MappingException: You may only speci ...

  5. ERROR: Field 'PostId' doesn't have a default value Exception in thread "main" org.hibernate.exception.GenericJDBCException: could not execute statement

    例子: Post p = new Post(); p.setPostId(3); p.setPostName("技术"); 在执行数据保持时提示session.save(p); 的 ...

  6. hibernate添加数据时Exception in thread "main" org.hibernate.PropertyValueException: not-null property references a null or transient value: com.javakc.hibernate.test.entity.TestEntity.testName

    意思是,一个非null属性引用了一个null或瞬态值.就是在对应实体类配置文件hbm.xml中该属性配置了not-null="true",将其去掉即可.

  7. Exception in thread "main" org.hibernate.MappingException: Unknown entity: com.mao.PersonSet

    转自:https://blog.csdn.net/vipmao/article/details/51334743

  8. 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 ...

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

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

随机推荐

  1. Linux中mysql乱码问题

    注意: 关于utf8和gbk的区别详细见:linux中文乱码问题解决办法 http://www.linuxidc.com/Linux/2010-04/25757.htm ,下面的配置中根据自己要求选择 ...

  2. 教程:基于访问控制的ABAP CDS视图权限

    Hi! 对每一个CDS视图,我们都可以通过DCL(Data Control Language)定义访问控制.在这篇文章中,我会介绍ABAP CDS视图中非常重要的一面:权限管理. 本文的阐述基于我正在 ...

  3. centos上安装配置java WEB环境_java(转)

    趁着十一期间,好好的写写随笔来记录自己所学.所践和所得,不足之处,欢迎各位拍砖~~~ 工具:Xftp 5.Xshell 5 一.安装jdk 1. 使用Xftp 5把jdk-8u65-linux-x64 ...

  4. 新瓶装旧酒:全程无命令 GitHub Pages 创建您的博客站点

    使用 GitHub Pages 创建博客站点的文章很多,也有很长的历史了.但是,许多已经与当前的 GitHub 不一致了,如果你按图索骥,会发现驴唇对不上马嘴. 更为麻烦的是,你会发现或者需要你输入许 ...

  5. HTML5-移动开发常用技巧与弹性布局的使用

    一.移动开发常用技巧 Viewport基本知识 设置布局Viewport的各种信息 1.width=device-width: 设置Viewport视口宽度等于设备宽度 2.initial-scale ...

  6. VMware Workstation 12 Pro 之安装林耐斯-LinuxMint-系统

    VMware Workstation 12 Pro 之安装林耐斯-LinuxMint-系统... ---------------------- ---------------------------- ...

  7. C++读取csv表格文件到vector

    这个CSV文件假设知道每行有多少个数,也知道数据的格式,即可使用下面简单的方法实现. 我们假设每行有4个数据,依次是int,int,float,float 基本思路是:把每行的数据定为一个类型,放在v ...

  8. 虚拟机下安装VMware Tools

    使用虚拟机安装第二操作系统,是我们经常使用到的一种方法,但是为了能够在虚拟机与本地操作系统间进行文件共享,我们可以借助一款非常实用的工具VMware Tools.下面我们就来说一下怎样安装VMware ...

  9. jenkins+ant+jmeter接口自动化测试(持续构建)

    使用badboy录制脚本,到处到jmeter后进行接口自动化,后来想着 可不可以用自动化来跑脚本呢,不用jmeter的图形界面呢, 选择了ant来进行构建,最后想到了用Jenkins来进行持续构建接口 ...

  10. 官方问答--微信小程序常见FAQ (17.8.21-17.8.27)

    给提问的开发者的建议:提问之前先查询 文档.通过社区右上角搜索搜索已经存在的问题. 写一个简明扼要的标题,并且正文描述清楚你的问题. 提交 BUG:需要带上基础库版本号,设备信息(iOS, Andro ...