java - 插入时休眠异常
问题描述
我有两个表应用程序,先生。我正在使用 JPA、Hibernate 进行 ORM 映射。插入记录时出现问题。请帮我。
* Mr_id in application table is a foreign key
* code in mr table is unique key
* mr_id in MR table is primary key
申请表:
(id, mr_id)
(1,空)
(2,空)
先生表:
(mr_id、代码、名称)
(1,code1,mr1)
(2、code2、mr2)
I have a jpa repository : ApplicationRepository
application = Application(1)
application.mr = MR(1,code1,mr1)
when I run : applicationRepository.save(applications[0])
it causes a problem
Reason: Mr record with (1,code1,mr1) alredy present in mr table.
How to solve this problem with JPA, Hibernate annotation
---------------------------------
Application {
@Id
var id: Int;
@ManyToOne(cascade = [(CascadeType.PERSIST)])
@JoinColumn(name = "mr_id")
var mr: MR? = null
}
解决方案
我认为发生这种情况是因为 JPA 试图在持久应用程序之后保留 MR(因为它们已分离),但 mr_id = 1 的 MR 已经存在。尝试使用 mr_id = 1(不是创建)找到 mr 并将此 mr 设置为 application.mr
推荐阅读
- neo4j - 关系记录的布尔属性“firstInFirstChain”和“firstInSecondChain”是什么意思?
- java - 无法执行任务:下载项目时任务已执行错误
- javascript - 在 Vue.js 和 Node.js 开发环境中测试会话
- opengl - 将图像句柄转换为连贯 uimage2D 的正确方法是什么
- u-sql - 使用 U-SQL 解析多级 Json
- javascript - 如何从 vue 路由器推送 html 页面,而不是路由到多页面模式 Vue 应用程序中的另一个 vue 组件?
- loops - SAS:如何遍历数据集以替换从另一个数据集中的列中读取的(大)字符串列表?
- kubernetes - Kubernetes 上的 Prometheus 存储
- python - 使用 pandas 从 timedelta-values 创建新的唯一会话 ID
- otree - oTree - 显示为按钮的布尔字段