JPA object references an unsaved transient instance - save the transient instance before flushing
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的更多相关文章
- 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(参考资料: ...
- 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 ...
- object references an unsaved transient instance save the transient instance before flushing
object references an unsaved transient instance save the transient instance before flushing 对象引用未保存的 ...
- Hibernate的一个问题object references an unsaved transient instance - save the transi5
1 我做了一对多和多对一的双向关联关系,在User这一方@ManyToOne已经设置了级联Cascade,这样在测试程序中保存User时,Group也应该自动保存,为什么会抛出以下的异常: (我是按着 ...
- 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 ...
- object references an unsaved transient instance - save the transient instance before flushing异常问题处理
一.异常:org.hibernate.TransientObjectException: object references an unsaved transient instance - save ...
- ManyToMany【项目随笔】关于异常object references an unsaved transient instance
在保存ManyToMany 时出现异常: org.springframework.dao.InvalidDataAccessApiUsageException: org.hibernate.Tran ...
- 三大框架常遇的错误:hibernate : object references an unsaved transient instance
hibernate : object references an unsaved transient instance 该错误是操作顺序的问题,比如: save或update顺序问题---比方学生表和 ...
- object references an unsaved transient instance【异常】
[异常提示] TransientObjectException: object references an unsaved transient instance -save the transient ...
- org.unsaved transient instance - save the transient instance before flushing: bug解决方案
最近开发和学习过程中,遇到很多零碎的知识点,在此简单地记录下: 1.遇如下bug: org.unsaved transient instance - save the transient instan ...
随机推荐
- strimzi实战之二:部署和消息功能初体验
欢迎访问我的GitHub 这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos 本篇概览 本篇是<strimzi实战>系列 ...
- 低功耗引擎 Cliptrix 有什么价值
在万物互联的时代,现代人已普遍接受电视.音箱等电器设备具备智能化能力,也是在这个趋势下,我们身边越来越多的iOT设备联网和交互成为刚需.但iot设备也面临到一些非常显著的痛点,例如iot设备的内存.处 ...
- Python基础——函数的理解、函数对象、函数嵌套、闭包函数、及其应用
文章目录 函数也是变量 可以赋值 可以当做函数当做参数传给另外一个函数 可以当做函数当做另外一个函数的返回值 可以当做容器类型的一个元素 函数对象应用示范 原始版 修正版 函数嵌套 函数的嵌套调用 函 ...
- 【matplotlib 实战】--直方图
直方图,又称质量分布图,用于表示数据的分布情况,是一种常见的统计图表. 一般用横轴表示数据区间,纵轴表示分布情况,柱子越高,则落在该区间的数量越大.构建直方图时,首先首先就是对数据划分区间,通俗的说即 ...
- 2023_10_09_MYSQL_DAY_01_笔记
2023_10_09_MYSQL_DAY_01 #运算符的优先级 SELECT ename, job, sal FROM emp WHERE ( job='SALESMAN' OR job='PRES ...
- 如何在 Vue.js 中引入原子设计?
本文为翻译文章,原文链接: https://medium.com/@9haroon_dev/introducing-atomic-design-in-vue-js-a9e873637a3e 前言 原子 ...
- Kubernetes 迁移节点 Kubelet 数据存储目录
1.概述 默认Kubernetes节点Kubelet数据目录在/var/lib/kubelet,如果在部署前没有做好规划,其实默认就存储在系统盘/分区下了,这可能会引发一些问题: 磁盘空间限制: 系统 ...
- Asp.Net Core webapi+net6 使用资源筛选器(过滤器) 做缓存
写一个特性类,用来做标记 [AttributeUsage(AttributeTargets.Method)] //只对方法有效 public class ResourceFilterAttribute ...
- JavaScript 简介与引用
作者:WangMin 格言:努力做好自己喜欢的每一件事 我们通常写好的HTML网页是处于一个静态的效果,在用户体验这一方面就不是很好,给人一种死板的感觉.这里我们就可以用到JavaScript来为网页 ...
- 01Java学习_概述
概述 目录 概述 三个阶段 概述 历史(了解) Java 重要特点 开发工具 Java 运行机制及运行过程 下载和安装 JDK 配置环境变量 java 快速入门 博客中的图片内容全部采用 github ...