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 ...
随机推荐
- visio 2010 kit tools
Getting Office License Configuration Information.---------------------------------------Backing Up L ...
- 是因为不同的浏览器内核吗--Could not register service workers到底是怎么回事
什么是浏览器内核 浏览器内核(Rendering Engine),是浏览器最核心的部分. 它负责处理网页的HTML.CSS.JavaScript等代码,并将其转化为可视化的网页内容.即我们常说的对网页 ...
- 用go封装和实现扫码登录
用go封装和实现扫码登录 本篇为用go设计开发一个自己的轻量级登录库/框架吧 - 秋玻 - 博客园 (cnblogs.com)的扫码登录业务篇,会讲讲扫码登录的实现,给库/框架增加新的功能,最后说明使 ...
- Go包介绍与初始化:搞清Go程序的执行次序
Go包介绍与初始化:搞清Go程序的执行次序 目录 Go包介绍与初始化:搞清Go程序的执行次序 一.main.main 函数:Go 应用的入口函数 1.1 main.main 函数 1.2 main.m ...
- 运行 Python 脚本/代码的几种方式
哈喽大家好,我是咸鱼 我们知道,python 脚本或者说 python 程序其实是一个包含了 python 代码的文件.要让它们实现特定功能,我们需要知道该如何运行(run)它 通过运行 python ...
- [ABC276Ex] Construct a Matrix
没有题解,所以来写一篇. Description 构造一个 \(N\times N\) 的矩阵 \(A\),其中 \(A_{i,j}\in {0,1,2}\),要求同时满足 \(Q\) 条限制. 每条 ...
- Python 批量合并图片到word文档
这段代码是一个用Python编写的功能,它将指定文件夹中的所有图片插入到Word文档中并保存.以下是代码的主要步骤和功能: 导入必要的库 Python中的docx库用于操作Word文档,glob库用于 ...
- Dapper.Lite 扩展
最近重构并精简了Dapper.Lite,然后把不依赖Dapper的版本LiteSql也重构了一下,和Dapper.Lite保持一致.感觉这两款ORM基本完工,自荐一下. .NET的ORM虽多,堪用的不 ...
- Nebula Graph开源分布式图数据库,万亿级数据,毫秒级延时
推荐一个分布式图数据库Nebula Graph,万亿级数据,毫秒级延时 什么是Nebula Graph Nebula Graph 是一款开源的.分布式的.易扩展的原生图数据库,能够承载包含数千亿个点和 ...
- 2021CSP 游记
总结 试机日: 我天,这学校什么垃圾电脑-- 比赛日: 1. 普及考试 总体上来说题目算简单 (只是我脑残),t1简单 \(O(1)\),学了数论就行,而 t2 看懂后按题意打一遍,再优化一下: 数组 ...