0

I am getting the following error when i try to insert into the table "Schema1"."REQST":

[2017-08-30 21:10:39,869] ERROR [SqlExceptionHelper]Cannot add or update a  child row: 
a foreign key constraint fails ("Schema1"."REQST", CONSTRAINT 
"FK" FOREIGN KEY ("REQST_CD") REFERENCES "WEB_REQST_CD" ("RESP_CD") ON DELETE NO ACTION ON UPDATE NO ACTI) 

Here is the mapping files for the table :

TRAN_REQST table:

<many-to-one name="reqstCd" class="WebReqstCdEntity" 
        fetch="select" update="false" insert="false">
        <column name="REQST_CD"  length="3" />
    </many-to-one>

This is unidirectional. There is no mapping from RESP_CD table. WEB_REQST_CD is a code table and I do not want values to be persisted. Hence update and insert is false.

Before saving to TRAN_REQST's entity object,I saved ReqstCdEntity :

   ReqstEntity reqEntity = new ReqEntity();
    ReqstCdEntity reqstCdEntity = new ReqstCdEntity();
    reqstCdEntity.setCodeNumber("100");
    session.save(respCdEntity);
    reqEntity.setCode(reqstCdEntity);
    session.save(reqEntity)

I also noticed in the hibernate query to insert in REQST table is incorrect, It does not contain the REQST_CD column at all. Hibernate query:

insert into TRAN_REQST (REQ_ID, TIME) values(?,?)

I tried the following troubleshooting steps but nothing worked:

-- Tried adding cascade settings "save-update" but did not help.

-- Made sure the entity objects are formed properly.

--Tried dropping Foreign key constraint and adding it.

Any help will be greatly appreciated!

SRm
  • 49
  • 9
  • This should help you https://stackoverflow.com/questions/1253459/mysql-error-1452-cannot-add-or-update-a-child-row-a-foreign-key-constraint-fa Let me know if you checked this faq and you need additional help – Noob Aug 31 '17 at 21:56
  • I have tried most of those solutions. Nothing seems to help. Hibernate query does not save the Reqst_Cd object for some reason. Editing the post to include the code. – SRm Sep 01 '17 at 18:37

0 Answers0