JPA and Foreign key constraint

C

carmelo

Hi everybody,
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)

- 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.MySQLIntegrityConstraintViolationException:
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:
)



How can I do? Please help me.

Thank you very much in advance for your help
 
L

Lew

carmelo said:
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.MySQLIntegrityConstraintViolationException:
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:
)

How can I do? Please help me.

Thank you very much in advance for your help[/QUOTE]

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?
 
J

Jean-Baptiste Nizet

carmelo a écrit :
Hi everybody,
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)

- 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.MySQLIntegrityConstraintViolationException:
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:
)



How can I do? Please help me.

Thank you very much in advance for your help[/QUOTE]

You don't tell us enough to diagnose the problem. I've had the same kind 
of problem using Hibernate, if the many-to-one association is not marked 
optional=false or the join column is not marked nullable=false(I don't 
remember which).
In this case Hibernate tried to insert the two records, and then update 
the details one with the appropriate value for the foreign key. Maybe 
TopLink does the same.

JB.
 
C

carmelo

Thank you guys for your answers.

I'll try to give you more details...

My MySQL database is of InnoDB type.

'master'.'master_code' has a UNIQUE constraint.

- on details:
@JoinColumn(name = "master_code", referencedColumnName =
"master_code", nullable = false)
@ManyToOne(optional = false)

- on master:
@OneToMany(cascade = CascadeType.ALL, mappedBy = "master_code")


I hope you can help me now. This is quite a while I'm trying to solve
it...

Thank you very much again
 
L

Lew

carmelo said:
Thank you guys for your answers.

I'll try to give you more details...

My MySQL database is of InnoDB type.

'master'.'master_code' has a UNIQUE constraint.

- on details:
@JoinColumn(name = "master_code", referencedColumnName =
"master_code", nullable = false)
@ManyToOne(optional = false)

- on master:
@OneToMany(cascade = CascadeType.ALL, mappedBy = "master_code")

Are you able to construct and post an SSCCE
<http://sscce.org/>
that demonstrates your difficulty?
 
S

SergeyT

As others already told you (but in other words) you are trying to save
a detail entity without prior saving the master one. For example, you
created a master entity object, but didn't call manager.persist() on
it, and then tried to persist the detail entity.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,189
Members
46,734
Latest member
manin

Latest Threads

Top