nested exception is org.springframework.dao.InvalidDataAccessApiUsageException: org.hibernate.TransientPropertyValueException: object references an unsaved transient instance - save the transient instance before flushing : top.lingkang.jpademo.entity.UserRole.role -> top.lingkang.jpademo.entity.Role; nested exception is java.lang.IllegalStateException: org.hibernate.TransientPropertyValueException: object references an unsaved transient instance - save the transient instance before flushing : top.lingkang.jpademo.entity.UserRole.role -> top.lingkang.jpademo.entity.Role] with root cause

当你用jpa或者hibernate进行save时,报以上错误说明你对关联对象进行了修改或者这个管理对象不存在于表中。
例如下面的

/**
* @author lingkang
* Created by 2022/7/13
*/
@Data
@Entity
@Table(name = "j_user_role")
public class UserRole {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)//主键递增策略
private Integer id; @ManyToOne
@JoinColumn(name = "user_id")
private User user; @ManyToOne
@JoinColumn(name = "role")
private Role role; @Column
private String remark;
}

你想直接save一个对象:

    @Transactional
@GetMapping("add")
public Object add(){
UserRole userRole=new UserRole();
Role role=new Role();
role.setRole("user");// 随意搞个角色
role.setDescription("普通用户");
userRole.setRole(role);
userRoleRepository.save(userRole);
return userRole;
}

就会报以上错误,因为关联关系的值在数据库中不能为空,这里的role角色表少了user这角色导致的。你应该先新增一个角色,否则关联关系不成立。hibernate是一个强关联的ORM,于是改成下面这样就成功了:

    @Transactional
@GetMapping("add")
public Object add(){
UserRole userRole=new UserRole();
Role role=new Role();
role.setRole("user");// 随意搞个角色
role.setDescription("普通用户");
roleRepository.save(role);// 先保存
userRole.setRole(role);
userRoleRepository.save(userRole);
return userRole;
}

使用hibernate的关联关系时,尤其要注意关联的对象一定要存在于数据库。

当关联对象不存在,你自己new一个时,他会将你的new的对象持久化

    @Transactional
@GetMapping("add2")
public Object add2(){
List<UserRole> all = userRoleRepository.findAll();
for (UserRole role:all){
if (role.getUser()==null){// 关联对象不存在时,他会将你的new的对象持久化
User user=new User();
user.setUsername("999999999999999999999999");
role.setUser(user);
userRoleRepository.save(role);
}
}
return all;
}

查看用户表你会发现多了个数据

所以理清多对多关系比较重要,现在的数据库设计一般都是非强关联的,甚至连个外键都没有。一用hibernate深似海,从此强行联系在一起。

hibernate性能比不上mybatis,多一步查询关联关系(可能会全表扫描),特别是数据极多的时候(一百万以上),性能成吨下降。

JPA object references an unsaved transient instance - save the transient instance before flushing的更多相关文章

  1. object references an unsaved transient instance - save the transient instance before flushing错误

    异常1:not-null property references a null or transient value解决方法:将“一对多”关系中的“一”方,not-null设置为false(参考资料: ...

  2. 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 ...

  3. object references an unsaved transient instance save the transient instance before flushing

    object references an unsaved transient instance save the transient instance before flushing 对象引用未保存的 ...

  4. Hibernate的一个问题object references an unsaved transient instance - save the transi5

    1 我做了一对多和多对一的双向关联关系,在User这一方@ManyToOne已经设置了级联Cascade,这样在测试程序中保存User时,Group也应该自动保存,为什么会抛出以下的异常: (我是按着 ...

  5. object references an unsaved transient instance - save the transient instance before flushing: com.jspxcms.core.domain.ScTeam

    object references an unsaved transient instance - save the transient instance before flushing: com.j ...

  6. object references an unsaved transient instance - save the transient instance before flushing异常问题处理

    一.异常:org.hibernate.TransientObjectException: object references an unsaved transient instance - save ...

  7. ManyToMany【项目随笔】关于异常object references an unsaved transient instance

    在保存ManyToMany  时出现异常: org.springframework.dao.InvalidDataAccessApiUsageException: org.hibernate.Tran ...

  8. 三大框架常遇的错误:hibernate : object references an unsaved transient instance

    hibernate : object references an unsaved transient instance 该错误是操作顺序的问题,比如: save或update顺序问题---比方学生表和 ...

  9. object references an unsaved transient instance【异常】

    [异常提示] TransientObjectException: object references an unsaved transient instance -save the transient ...

  10. org.unsaved transient instance - save the transient instance before flushing: bug解决方案

    最近开发和学习过程中,遇到很多零碎的知识点,在此简单地记录下: 1.遇如下bug: org.unsaved transient instance - save the transient instan ...

随机推荐

  1. vue中watch侦听器,deep和immediate的用法

    1.deep深度监听的用法 当监听一个对象时,可能想监听整个对象的变化,而不仅仅是某个属性.但在默认情况下,如果你正在监听formData对象并且修改了formData.username,对应的侦听器 ...

  2. oracle监听配置与防火墙问题

    在建好pdb容器后,需配置网络,才能从客户端连接服务器端 1.首先查看pdb容器的服务名 lsnrctl status ... Service "19cdb" has 1 inst ...

  3. Oracle查询--增加--删除--修改主键

    对Oracle表主键的操作,有四类:查询,增加,修改,删除 1.查询主键 /*查询某个表中存在的约束*/ select * from user_constraints where table_name ...

  4. 使用 TensorFlow 进行机器学习

    使用 TensorFlow 进行机器学习 这是使用 TensorFlow 进行机器学习的官方代码存储库. 使用 TensorFlow(Google 最新.最好的机器学习库)开始进行机器学习. 概括 第 ...

  5. 【Unity3D】水面特效

    1 前言 ​ 水波特效 中通过屏幕后处理实现了环形水波效果,本文通过 Shader Graph 实现了模拟水面特效,包含以下特效细节.Shader Graph 基础知识详见→Shader Graph简 ...

  6. tunm二进制协议在python上的实现

    tunm二进制协议在python上的实现 tunm是一种对标JSON的二进制协议, 支持JSON的所有类型的动态组合 支持的数据类型 基本支持的类型 "u8", "i8& ...

  7. YXの每日挂分记录

    7.11 T1 不开两倍数组 100->60. 7.18 T2 dp+矩乘 转移不判边界 100->10. 7.20 T2 人类智慧 1e6 n log n 100->10,求前 5 ...

  8. go中的内存逃逸

    内存逃逸(memory escape)是指在编写 Go 代码时,某些变量或数据的生命周期超出了其原始作用域的情况.当变量逃逸到函数外部或持续存在于堆上时,会导致内存分配的开销,从而对程序的性能产生负面 ...

  9. 你还在为SFTP连接超时而困惑么?

    1. 前言 在最近的项目联调过程中,发现在连接上游侧SFTP时总是需要等待大约10s+的时间才会出现密码输入界面,这种长时间的等待直接导致的调用文件接口时连接sftp超时问题.于是决定自己针对该问题进 ...

  10. 数据结构与算法 | 动态规划算法(Dynamic Programming)

    上一篇文末已经提到了记忆化搜索是动态规划(Dynamic Programming)的一种形式,是一种自顶向下(Top-Down)的思考方式,通常采用递归的编码形式:既然动态规划有自顶向下(Top-Do ...