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

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. UI设计|PS软件操作应用——GIF动图

      前  言 JRedu 在之前的分享中,跟大家主要讲解了PS软件工具的基本操作,对主要的图层.蒙版.通道和滤镜都有一些介绍,希望对大家有所帮助,在介绍这些工具时也提到过GIF,而在本次分享中就跟大家 ...

  2. Linux学习总结(九)—— CentOS常用软件安装:中文输入法、Chrome

    中文输入法 切换到root用户,运行以下命令: yum install "@Chinese Support" 按照提示,一路Yes等待安装完成.安装完成后点击左上角System - ...

  3. 我了解到的JavaScript异步编程

    一. 一道面试题 前段时间面试,考察比较多的是js异步编程方面的相关知识点,如今,正好轮到自己分享技术,所以想把js异步编程学习下,做个总结. 下面这个demo 概括了大多数面试过程中遇到的问题: f ...

  4. (转)CentOS无损调整磁盘分区大小的实现方法

    前几天在QQ群里,有一个朋友问到关于Linux中如何无损调整磁盘分区大小的问题,针对这个问题我在实际使用的过程中也曾探索过,所以比较关注如何无损的调整磁盘分区大小,而不丢失数据!在Windows中,我 ...

  5. 【Js应用实例】图片预览

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  6. Google reCAPTCHA 人机身份验证

    1. 适合范围 注册及登录某些使用Google验证码的网站,如https://zaif.jp/,会要求进行人机身份验证 2. 方法介绍 下载老D的Google hosts以及一键更新批处理程序 老D博 ...

  7. Jquery Mobile笔记之一

    jQuery Mobile 在你的网页中添加 jQuery Mobile 你可以通过以下几种方式将jQuery Mobile添加到你的网页中: 从 CDN 中加载 jQuery Mobile (推荐) ...

  8. [2017-08-16]ABP系列——QuickStartB:正确理解Abp解决方案的代码组织方式、分层和命名空间

    本系列目录:Abp介绍和经验分享-目录 介绍ABP的文章,大多会提到ABP框架吸收了很多最佳实践,比如: 1.N层 (复用一下上篇的图) 展现层(Personball.Demo.Web):asp.ne ...

  9. Java 高级特性: Lambda 表达式

    本文源代码上传到了码云,请点击 LambdaExpression 获取.Lambda 表达式是 java 8 的新特性,本文讲解了 lambda 表达式的所有知识.内容涉及到 lambda 表达式是什 ...

  10. 实现CA证书创建及客户端申请证书

    author:JevonWei 版权声明:原创作品 CA证书的相关文件路径 openssl配置文件/etc/pki/tls/openssl.cnf /etc/pki/tls/openssl.cnf C ...