我有两个表的应用程序,先生我正在使用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表:
(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
}发布于 2018-06-28 14:44:55
我认为发生这种情况是因为JPA试图在persist之后持久化MR (因为它们是分离的),但是mr_id =1的MR已经存在。尝试查找mr_id =1(不是create)的mr,并将此mr设置为application.mr
https://stackoverflow.com/questions/51075112
复制相似问题