【问题标题】:Hibernate exception while inserting插入时休眠异常
【发布时间】:2018-12-07 01:51:19
【问题描述】:

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

(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
    }

【问题讨论】:

  • 能否请您检查一下您的数据库是否可用?
  • 你能分享错误堆栈跟踪吗

标签: java spring hibernate kotlin spring-data-jpa


【解决方案1】:

我认为发生这种情况是因为 JPA 试图在持久化应用程序之后持久化 MR(因为它们已分离),但 mr_id = 1 的 MR 已经存在。尝试找到 mr_id = 1(不是创建)的 mr 并将此 mr 设置为 application.mr

【讨论】:

    猜你喜欢
    • 2013-12-31
    • 2011-02-14
    • 2014-06-16
    • 2023-03-17
    • 2014-08-22
    • 2011-09-20
    • 2017-07-06
    • 2012-02-24
    • 1970-01-01
    相关资源
    最近更新 更多