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实战之三:prometheus+grafana监控(按官方文档搞不定监控?不妨看看本文,已经踩过坑了)
欢迎访问我的GitHub 这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos 本篇概览 由于整个系列的实战都涉及到消息生产和消费,所 ...
- 洛谷题解 | P1051 谁拿了最多奖学金
目录 题目描述 输入格式 输出格式 输入输出样例 提示 题目思路 AC代码 题目描述 某校的惯例是在每学期的期末考试之后发放奖学金.发放的奖学金共有五种,获取的条件各自不同: 1. 院士奖学金,每人 ...
- nginx配置kibana访问用户名和密码认证、及无认证访问配置
转载请注明出处: 在nginx上配置kibana页面访问时,默认是采用kibana的认证,一般直接安装kibana后,是没有用户名和密码认证的. 如果要在负载均衡上配置反向代理和用户认证,可按以下步骤 ...
- 我看懂了oracle中pivot行转列的用法
我看懂了PIVOT的用法 用法Select * From 表名,PIVOT( SUM('要合并的列1'),MAX('要合并的列2'),....FOR 将值转换成列的列名 IN(列名1,列名2,列名3 ...
- FreeRTOS 操作系统
FreeRTOS操作系统 01 FreeRTOS 的定义和概述 定义:FreeRTOS(Free-Real-Time Operating System)是一个开源的实时操作系统内核,专门为嵌入式系统设 ...
- 文心一言 VS 讯飞星火 VS chatgpt (109)-- 算法导论10.1 7题
七.用go语言,说明如何用两个队列实现一个栈,并分析相关栈操作的运行时间. 文心一言: 在Go语言中,我们可以用两个队列(queue1和queue2)实现一个栈.以下是实现代码: package ma ...
- 【译】A unit of profiling makes the allocations go away
在 Visual Studio 17.8 Preview 2 中,我们更新了单元测试分析,允许你在性能分析器中使用任何可用的工具--而不仅仅是仪表工具.有了这个更改,可以很容易地快速分析孤立的小工作单 ...
- mysql学习之数据备份和恢复
一.使用mysqldump进行备份 如:将test数据库备份到/tmp/mysql_back/目录下 [root@localhost tmp]# mysqldump -uroot -p111 -l - ...
- 虹科分享 | 一起聊聊Redis企业版数据库与【微服务误解】那些事儿!
如今,关于微服务依然存在许多误解,企业盲目追求这种炫酷技术并不可取.同时,这种盲目行为对于希望用微服务来有效解决问题的公司很不利.不是说任何特定的技术都是缺乏实际价值的,如微服务.Kubernetes ...
- Unity学习笔记--基础
基础 3D数学 Mathf函数库 print(Mathf.PI); print(Mathf.Abs(-10)); print(Mathf.CeilToInt(1.2f));//向上取整 print(M ...