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

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. ctrl+c 和 ctrl+z 的区别

    ctrl+c 和 ctrl+z 的区别 ctrl+c和ctrl+z都是中断命令,但是他们的作用却不一样. ctrl+c是强制中断程序的执行, 而ctrl+z的是将任务中断,但是此任务并没有结束,他仍然 ...

  2. 【Regular Expression】RE分类及案例

    背景知识 正则表达式分为三类:基础正则表达式.扩展正则表达式.Perl正则表达式(Perl内建) 通俗来说,这三个一个比一个强大,支持的规则匹配字符更多 1.匹配IP ip addr | grep - ...

  3. 【HotSpot】 jps

    jps(1) General Commands Manual jps(1) Name jps - Java Virtual Machine Process Status Tool SYNOPSIS j ...

  4. 8.23.4 IO-输入输出16个流

    字节流: FileInputStream FileInputStream fIn = new FileInputStream("1.avi");   FileOutputStrea ...

  5. vue.js之获取当前点击对象(其实是套着vue的原生javascript吧,笑😊)

    转载请注明出处:http://www.cnblogs.com/meng1314-shuai/p/7455575.html 熟悉jquery的小伙伴应该都知道jquery获取当前点击对象是有多么的粗暴, ...

  6. MAC 上传文件到github

    在IOS中,经常需要上传文件到github.以桌面上的一个文件夹为例: 步骤1: cd 到该文件夹下,建立POD文件. $ cd /Users/andy/Desktop/openinstallSDK ...

  7. Java面向对象-方法的值传递和引用传递

    Java面向对象-方法的值传递和引用传递 0 发布时间:『 2016-08-21 14:21』  博客类别:Java核心基础  阅读(197) 评论(0) Java面向对象-方法的值传递和引用传递 方 ...

  8. 图标字体库(用CSS样式生成搜索、购物车等图标)

    参考网址:http://fontawesome.dashgame.com/ 基本图标: 您可以将Font Awesome图标使用在几乎任何地方,只需要使用CSS前缀 fa ,再加上图标名称. Font ...

  9. 文档API生成神器SandCastle使用心得

    一.功能描述 关于Sandcastle网上的参考资料相对较少,Google出来很多资料都是全英文的,相对于我这种英语渣渣看起来还是很费劲的. 言简意赅,Sandcastle主要功能是能够将C#类生成类 ...

  10. Akka(23): Stream:自定义流构件功能-Custom defined stream processing stages

    从总体上看:akka-stream是由数据源头Source,流通节点Flow和数据流终点Sink三个框架性的流构件(stream components)组成的.这其中:Source和Sink是stre ...