Exception in thread "main" org.hibernate.TransientObjectException: object references an unsaved tran
今天在使用一对多,多对一保存数据的时候出现了这个错误
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的更多相关文章
- 在运行Hibernate Hello World程序的时候,抛如下错误: view plain Exception in thread "main" org.hibernate.exception.LockAcquisitionException 解决方法
在运行Hibernate Hello World程序的时候,抛如下错误: Exception in thread "main" org.hibernate.exception.Lo ...
- Exception in thread "main" org.hibernate.HibernateException: save is not valid without active transaction
在spring4+hibernate4整合过程中,使用@Transactional注解事务会报"Exception in thread "main" org.hibern ...
- 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 ...
- 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 ...
- 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); 的 ...
- 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",将其去掉即可.
- Exception in thread "main" org.hibernate.MappingException: Unknown entity: com.mao.PersonSet
转自:https://blog.csdn.net/vipmao/article/details/51334743
- 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 ...
- Hibernate的一个问题object references an unsaved transient instance - save the transi5
1 我做了一对多和多对一的双向关联关系,在User这一方@ManyToOne已经设置了级联Cascade,这样在测试程序中保存User时,Group也应该自动保存,为什么会抛出以下的异常: (我是按着 ...
随机推荐
- ios逆向过程中lldb调试技巧
在ios逆向过程中,善于运用lldb,会给逆向带来很大的方便 一般的命令: 1.image list -o -f 看看各个模块在内存中的基址 2.register read r0 读取寄存器r0的 ...
- win10 + Debian9.1双系统安装笔记
今天去163镜像上下载了"linuxmint-18.2-cinnamon-64bit.iso"和win10安装了双系统,感觉比在桌面方面十分不错,下面mark一下自己安装过程中的总 ...
- Struts2学习笔记(三)——Action详解
Action是用于处理请求操作的,它是由StrutsPrepareAndExceuteFilter分发过来的. 1.Action的创建方式 1) POJO类(PlainOldJavaObjects简单 ...
- Basic Skill
Get current script path script=$() Check for root user ] ; then echo "current user is not root ...
- 【可视化】div背景半透明
css实现元素半透明使用 opacity:0.x 实现背景色半透明:rgba(a,b,c,x); x为透明度0,到1
- Django 学习笔记(三)模板导入
本章内容是将一个html网页放进模板中,并运行服务器将其展现出来. 平台:windows平台下Liunx子系统 目前的目录: hello ├── manage.py ├── hello │ ├── _ ...
- 一起学ASP.NET Core 2.0学习笔记(二): ef core2.0 及mysql provider 、Fluent API相关配置及迁移
不得不说微软的技术迭代还是很快的,上了微软的船就得跟着她走下去,前文一起学ASP.NET Core 2.0学习笔记(一): CentOS下 .net core2 sdk nginx.superviso ...
- 入侵拿下DVBBS php官网详细过程(图)
几 个月前,DVBBS php2.0暴了一个可以直接读出管理员密码的sql注入漏洞,当时这个漏洞出来的时候,我看的心痒,怎么还会有这么弱智的漏洞,DVBBS php2.0这套代码我还没仔细看过,于是5 ...
- 泛型集合转化为DataTable
public class DataTableUtil { /// <summary> /// 泛型集合转化为dataTable /// </summary> /// <t ...
- 原创: rsync软件服务利用ansible实现一键化部署
---恢复内容开始--- 首先创建一个脚本文件 /server/tools/peizhi.sh cat /server/tools/peizhi.sh cat >>/etc/rsyncd ...