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

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日期函数 当前日期 curdate() , 当前年 year(curdate()), 取date的年份 year(date) ,取date的月份 month(date)

    获取系统当前日期时间: sysdate() 获取系统当前日期:curdate() 获取系统当前时间:curtime() 获取给定日期的年份: year(date) 获取给定日期的月份:month(da ...

  2. Clang之词法分析Lex

    Clang是LLVM编译器框架的前端(Frontend)编译器,可编译链接C.C++.Objective-C和Objective-C++四种语言的项目代码.Clang 的开发目标是提供一个可以替代 G ...

  3. 第4天:JS入门-给div设置宽高背景色

    今天学习了js入门课程,听的不多,做了个小练习,给div设置宽高.背景色.一点点都是进步.核心代码如下: <!DOCTYPE html><html lang="en&quo ...

  4. @media实现网页自适应中的几个关键分辨率

    不同分辨率设备或不同窗口大小下网页布局经常是不同的,一不小心就会发生错位.可以利用@media screen实现网页布局的自适应,但是怎样兼容所有主流设备就成了问题.到底分辨率是多少的时候设置呢?首先 ...

  5. matplotlib 填充颜色

    def huitu_host(nodes,total): x = np.arange(len(nodes)) plt.figure(figsize=(9,5)) plt.xticks(x,nodes) ...

  6. open文件操作之mode模式剖析

    Python可以使用open函数来实现文件的打开,关闭,读写操作: Python3中的open函数定义为: open(file, mode='r', buffering=None, encoding= ...

  7. java创建泛型数组

    java中创建泛型数组并不是不可能,创建泛型数组通过反射,给构造函数传递两个参数,一个类型标记,一个数组大小.' 简单Demo如下: import java.lang.reflect.Array; / ...

  8. 查询session内容

    Enumeration enumsession = request.getSession().getAttributeNames(); while(enumsession.hasMoreElement ...

  9. django框架(Model)

    -------------------使用MySql数据库-------------------1.进行对应mysql-python包的下载 pip install mysql-python 2.在m ...

  10. QT creator编程C++第一步,说“Hello world!”

    这个学期选了计算机学院的<数字图像处理>,正好和我的图像识别项目有所关联,老师说不能用MATLAB来做,这让我一个没学过C++的孩纸欲哭无泪. 只好求助计算机学院的大佬,自学C++. 大佬 ...