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

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. MySql分库分表总结(转)

    为什么要分库分表 可以用说用到MySQL的地方,只要数据量一大, 马上就会遇到一个问题,要分库分表. 这里引用一个问题为什么要分库分表呢?MySQL处理不了大的表吗? 其实是可以处理的大表的.我所经历 ...

  2. decimal扩展方法(转换为字符串,去掉末尾的0)

    /// <summary> /// 转换为字符串,去掉末尾0 /// </summary> /// <param name="target">被 ...

  3. 【Ubuntu 16】 wifi连接 并解决无桌面图标问题

    笔记本上装了win10和ubuntu16双系统,ubuntu16有半年多没使用了,今天一登录成功后,没有桌面啦,一个干净的壁纸映入眼帘,真操蛋. 上网搜索后总结:应该是应用软件中心出了问题,可是,没法 ...

  4. sysctl -p 报错问题的解决方法

    最近执行sysctl -p 命令时一直报错,类似这种格式: error: permission denied on key...... 经过网上搜索, 原来这些问题都是因为openvz模版的问题,要进 ...

  5. css中滚动条样式的设置

    参数说明: 1.overflow-y : 设置当对象的内容超过其指定高度时如何管理内容:overflow-x : 设置当对象的内容超过其指定宽度时如何管理内容. 参数: visible:扩大面积以显示 ...

  6. 【JBoss】数据库连接配置小结(转)

    数据库驱动位置: %JBOSS_HOME%\server\default\lib目录下. 数据库配置文件位置:JBOSS_HOME\docs\examples\jca\XXXX-ds.xml < ...

  7. 基于SSM框架的文件上传

    我整理了一下,这个办法在Servlet上应该也用得上.其实文件上传主要的问题是在于存储路径的拼接. 这里我是把东西存在Web项目的WebContent目录下的 下面贴一些代码 目标应该是这样一个格式 ...

  8. Html5笔记之第八天

    HTML字符实体 显示结果 描述 实体名称 实体编号   空格     < 小于号 < < > 大于号 > > & 和号 & & " ...

  9. Java的HashMap实现原理整理总结

    通过Debug 探寻Java-HashMap 实现原理: 一个简单的例子,代码如下, 测试方法 main: public static void main(String[] args) { KeyOb ...

  10. [知了堂学习笔记]_纯JS制作《飞机大战》游戏_第2讲(对象的实现及全局变量的定义)

    整体展示: 一.全局变量 /*===================玩家参数==========================*/ var myPlane; //英雄对象 var leftbtn = ...