gpt4 book ai didi

java - 插入时出现 Hibernate 异常

转载 作者:行者123 更新时间:2023-12-02 11:04:35 26 4
gpt4 key购买 nike

我有两个表应用程序,先生。我使用 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,空)

先生表:

(先生 ID、代码、姓名)

(1,代码1,mr1)

(2,代码2,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

关于java - 插入时出现 Hibernate 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51075112/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com