0

I have two tables Application, MR. I am using JPA, Hibernate for ORM mapping. I have a problem while inserting the records. Please help me.

* Mr_id in application table is a foreign key

* code in mr table is unique key

* mr_id in MR table is primary key

Application Table:

(id, mr_id)

(1, null)

(2, null)

MR TABLE:

(mr_id, code, name)

(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
    }
2
  • Can you please check your Database if it is available or not already? Commented Jun 28, 2018 at 6:24
  • can you share the error stack trace Commented Jun 28, 2018 at 6:25

1 Answer 1

1

I think this happens because JPA is trying to persist MR (because they detached) after persist Application, but MR with mr_id = 1 already exists. Try to find mr with mr_id = 1 (not create) and set this mr to application.mr

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.