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. Redis系列内容完整版

    @ 目录 Redis系列之_Redis介绍安装配置 第一章 redis初识 1.1 Redis是什么 1.2 Redis特性(8个) 1.3 Redis单机安装 1.3.1下载安装 1.3.2三种启动 ...

  2. Django框架项目之登录注册——1-登录注册页面、2 多方式登录、3-手机是否存在验证接口、4-腾讯云短信开发、5 短信验证码接口、6-短信登录接口、7-短信注册接口、8-前台登录注册修订

    文章目录 1-登录注册页面 模态登录组件 模态注册组件 导航条:结合实际情况完成样式 登录业务分析 多方式登录 验证码登录 注册业务分析 验证码注册 汇总 2 多方式登录 后台 插件 urls.py ...

  3. Python3中的printable

    import string characters = string.printable # printable 是用作字符串常量的预初始化字符串.里面包含所有的标点符号,数字 print(charac ...

  4. .Net核心级的性能优化(GC篇)

    1.前言 大部分人对于.Net性能优化,都停留在业务层面.或者简单的.Net框架配置层面.本篇来看下.Net核心部分GC垃圾回收配置:保留VM,大对象,独立GC,节省内存等.Net8里面有很多的各种G ...

  5. C# ConfigMan.cs

    public static class ConfigMan { public static string ReadKey(string key) { return ConfigurationManag ...

  6. Java中Synchronized的用法(转)

    原文:http://blog.csdn.net/luoweifu/article/details/46613015 作者:luoweifu 转载请标名出处 <编程思想之多线程与多进程(1)--以 ...

  7. [Python急救站]打印菱形

    打印菱形程序 row = int(input("请输入要打印的列数: ")) #打印上面三角 for i in range(row): # i控制行数 for j in range ...

  8. [Python急救站课程]猜拳游戏

    猜拳游戏 import random # 调用random函数库 while True: x = random.randint(0, 2) # 调用库里的randint函数使用随机数. print(& ...

  9. CSS 单行/多行文本溢出显示省略号(...)的实现

    作者:WangMin 格言:努力做好自己喜欢的每一件事 我们在项目开发的过程中也许都遇到过这样的问题:我们需要实现这样一个需求,在一个父级元素中隐藏一个可能过长的文本.而这个需求可以分解为两个,一个是 ...

  10. 树莓派4B使用串口登录的设置方法

    -特别提示- 本文具有时效性. 当前我使用的是pi4硬件, 镜像版本 raspberrypi 5.15.61 32位. 在我解决该问题的时候, 在网上查找了很多方法, 有些方法被实际测试发现是不行的. ...