Foreign Key Constraint Issues Involving One-To-Many and Many-To-One Relationship in EcliplseLink2.1.2
I'm encountering a 1452 error on MySQL 5.5.9 while running some JPA tutorial code that I pulled off the EclipseLink website (http://wiki.eclipse.org/EclipseLink/Examples/JPA/EmployeeXML), and I'm hoping someone has some insight.
I'm using the latest stable build for EclipseLink as of today from the main website: eclipselink-jpa-modelgen_2.1.2.v20101206-r8635.jar and javax.persistence_2.0.1.v201006031150.jar.
When I populate the database via CreateDatabase.java, I get the following error:
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (jpatutorial
.phone
, CONSTRAINT FK_PHONE_EMP_PH_ID
FOREIGN KEY (EMP_PH_ID
) REFERENCES EMPLOYEE
(EMP_ID
))
Error Code: 1452
Call: INSERT INTO PHONE (PH_ID, TYPE, AREA_CODE, P_NUMBER, EMP_PH_ID) VALUES (?, ?, ?, ?, ?)
bind => [25, Work, 613, 5558812, 23]
Query: InsertObjectQuery(Phone([Work] 613 - 5558812))
The relevant models here are an Employee table and a Phone table. There is many-to-one relationship between Phones and Employees. Part of the orm.xml is below:
<entity class="model.PhoneNumber">
<table name="PHONE" />
<attributes>
.
.
<many-to-one name="owner">
<join-column name="EMP_PH_ID" />
</many-to-one>
</attributes>
</entity>
<entity class="model.Employee">
<!-- secondary-table name="SALARY" /-->
<attributes>
<id name="id">
<column name="EMP_ID" />
<generated-value />
</id>
.
.
<one-to-many name="phoneNumbers" mapped-by="owner">
<cascade>
<cascade-all />
</cascade>
<private-owned />
</one-to-many>
.
.
</attributes>
</entity>
The code successfully creates the tables, creates address records for the employees, creates all the employee records, but then fails on inserting a phone record even though it has a valid FK reference to a employee record. I simplified the code so that it executes, persists and commits everything up to the point of the failure, but whether I execute the insert through JPA or manually by running a SQL script, I end up with the same 1452 error.
Interestingly, I found that if I dropped all the related Employee entities, and recreate them off create table definitions reversed engineered by MySQL workbench, I am able to successfully insert records into all the tables through JPA and manually via scripts. So, this suggests Eclipselink is creating the tables in some way that is producing a problem, and MySQL Workbench isn't capturing these problematic definitions in the reverse engineering. At this point, I'm out of ideas.
Append DDL from logging on FINEST:
[EL Finest]: sequencing connected, state is Preallocation_Transaction_NoAccessor_State
[EL Finest]: sequence : preallocation size 50
[EL Finest]: sequence SEQ_GEN: preallocation size 50
[EL Info]: file:/Users/dylantong/Documents/workspace/JPA Tutorial/bin/_employee login successful
[EL Finest]: Execute query DataModifyQuery(sql="ALTER TABLE PHONE DROP FOREIGN KEY FK_PHONE_EMP_PH_ID")
[EL Fine]: Connection(876256661)--ALTER TABLE PHONE DROP FOREIGN KEY FK_PHONE_EMP_PH_ID
[EL Finest]: Execute query DataModifyQuery(sql="ALTER TABLE EMPLOYEE DROP FOREIGN KEY FK_EMPLOYEE_MANAGER_ID")
[EL Fine]: Connection(876256661)--ALTER TABLE EMPLOYEE DROP FOREIGN KEY FK_EMPLOYEE_MANAGER_ID
[EL Finest]: Execute query DataModifyQuery(sql="ALTER TABLE EMPLOYEE DROP FOREIGN KEY FK_EMPLOYEE_ADDR_ID")
[EL Fine]: Connection(876256661)--ALTER TABLE EMPLOYEE DROP FOREIGN KEY FK_EMPLOYEE_ADDR_ID
[EL Finest]: Execute query DataModifyQuery(sql="ALTER TABLE PROJ_EMP DROP FOREIGN KEY FK_PROJ_EMP_EMP_ID")
[EL Fine]: Connection(876256661)--ALTER TABLE PROJ_EMP DROP FOREIGN KEY FK_PROJ_EMP_EMP_ID
[EL Finest]: Execute query DataModifyQuery(sql="ALTER TABLE PROJ_EMP DROP FOREIGN KEY FK_PROJ_EMP_PROJ_ID")
[EL Fine]: Connection(876256661)--ALTER TABLE PROJ_EMP DROP FOREIGN KEY FK_PROJ_EMP_PROJ_ID
[EL Finest]: Execute query DataModifyQuery(sql="ALTER TABLE PROJECT DROP FOREIGN KEY FK_PROJECT_LEADER_ID")
[EL Fine]: Connection(876256661)--ALTER TABLE PROJECT DROP FOREIGN KEY FK_PROJECT_LEADER_ID
[EL Finest]: Execute query DataModifyQuery(sql="ALTER TABLE LPROJECT DROP FOREIGN KEY FK_LPROJECT_PROJ_ID")
[EL Fine]: Connection(876256661)--ALTER TABLE LPROJECT DROP FOREIGN KEY FK_LPROJECT_PROJ_ID
[EL Finest]: Execute query DataModifyQuery(sql="DROP TABLE PHONE")
[EL Fine]: Connection(876256661)--DROP TABLE PHONE
[EL Finest]: Execute query DataModifyQuery(sql="CREATE TABLE PHONE (PH_ID INTEGER NOT NULL, TYPE VARCHAR(255) NOT NULL, AREA_CODE VARCHAR(255), P_NUMBER VARCHAR(255), EMP_PH_ID INTEGER, PRIMARY KEY (PH_ID, TYPE))")
[EL Fine]: Connection(876256661)--CREATE TABLE PHONE (PH_ID INTEGER NOT NULL, TYPE VARCHAR(255) NOT NULL, AREA_CODE VARCHAR(255), P_NUMBER VARCHAR(255), EMP_PH_ID INTEGER, PRIMARY KEY (PH_ID, TYPE))
[EL Finest]: Execute query DataModifyQuery(sql="DROP TABLE EMPLOYEE")
[EL Fine]: Connection(876256661)--DROP TABLE EMPLOYEE
[EL Finest]: Execute query DataModifyQuery(sql="CREATE TABLE EMPLOYEE (EMP_ID INTEGER NOT NULL, L_NAME VARCHAR(255), END_TIME TIME, VERSION BIGINT, START_TIME TIME, GENDER VARCHAR(255), salary DOUBLE, F_NAME VARCHAR(255), MANAGER_ID INTEGER, ADDR_ID INTEGER, START_DATE DATE, END_DATE DATE, PRIMARY KEY (EMP_ID))")
[EL Fine]: Connection(876256661)--CREATE TABLE EMPLOYEE (EMP_ID INTEGER NOT NULL, L_NAME VARCHAR(255), END_TIME TIME, VERSION BIGINT, START_TIME TIME, GENDER VARCHAR(255), salary DOUBLE, F_NAME VARCHAR(255), MANAGER_ID INTEGER, ADDR_ID INTEGER, START_DATE DATE, END_DATE DATE, PRIMARY KEY (EMP_ID))
[EL Finest]: Execute query DataModifyQuery(sql="DROP TABLE PROJ_EMP")
[EL Fine]: Connection(876256661)--DROP TABLE PROJ_EMP
[EL Finest]: Execute query DataModifyQuery(sql="CREATE TABLE PROJ_EMP (EMP_ID INTEGER NOT NULL, PROJ_ID INTEGER NOT NULL, PRIMARY KEY (EMP_ID, PROJ_ID))")
[EL Fine]: Connection(876256661)--CREATE TABLE PROJ_EMP (EMP_ID INTEGER NOT NULL, PROJ_ID INTEGER NOT NULL, PRIMARY KEY (EMP_ID, PROJ_ID))
[EL Finest]: Execute query DataModifyQuery(sql="DROP TABLE ADDRESS")
[EL Fine]: Connection(876256661)--DROP TABLE ADDRESS
[EL Finest]: Execute query DataModifyQuery(sql="CREATE TABLE ADDRESS (ADDRESS_ID INTEGER NOT NULL, P_CODE VARCHAR(255), STREET VARCHAR(255), PROVINCE VARCHAR(255), COUNTRY VARCHAR(255), CITY VARCHAR(255), PRIMARY KEY (ADDRESS_ID))")
[EL Fine]: Connection(876256661)--CREATE TABLE ADDRESS (ADDRESS_ID INTEGER NOT NULL, P_CODE VARCHAR(255), STREET VARCHAR(255), PROVINCE VARCHAR(255), COUNTRY VARCHAR(255), CITY VARCHAR(255), PRIMARY KEY (ADDRESS_ID))
[EL Finest]: Execute query DataModifyQuery(sql="DROP TABLE PROJECT")
[EL Fine]: Connection(876256661)--DROP TABLE PROJECT
[EL Finest]: Execute query DataModifyQuery(sql="CREATE TABLE PROJECT (PROJ_ID INTEGER NOT NULL, PROJ_TYPE VARCHAR(31), DESCRIP VARCHAR(255), PROJ_NAME VARCHAR(255), VERSION BIGINT, LEADER_ID INTEGER, PRIMARY KEY (PROJ_ID))")
[EL Fine]: Connection(876256661)--CREATE TABLE PROJECT (PROJ_ID INTEGER NOT NULL, PROJ_TYPE VARCHAR(31), DESCRIP VARCHAR(255), PROJ_NAME VARCHAR(255), VERSION BIGINT, LEADER_ID INTEGER, PRIMARY KEY (PROJ_ID))
[EL Finest]: Execute query DataModifyQuery(sql="DROP TABLE RESPONS")
[EL Fine]: Connection(876256661)--DROP TABLE RESPONS
[EL Finest]: Execute query DataModifyQuery(sql="CREATE TABLE RESPONS (EMP_ID INTEGER NOT NULL, RESPON_DESC VARCHAR(255))")
[EL Fine]: Connection(876256661)--CREATE TABLE RESPONS (EMP_ID INTEGER NOT NULL, RESPON_DESC VARCHAR(255))
[EL Finest]: Execute query DataModifyQuery(sql="DROP TABLE LPROJECT")
[EL Fine]: Connection(876256661)--DROP TABLE LPROJECT
[EL Finest]: Execute query DataModifyQuery(sql="CREATE TABLE LPROJECT (PROJ_ID INTEGER NOT NULL, MILESTONE DATETIME, BUDGET DOUBLE, PRIMARY KEY (PROJ_ID))")
[EL Fine]: Connection(876256661)--CREATE TABLE LPROJECT (PROJ_ID INTEGER NOT NULL, MILESTONE DATETIME, BUDGET DOUBLE, PRIMARY KEY (PROJ_ID))
[EL Finest]: Execute query DataModifyQuery(sql="ALTER TABLE PHONE ADD CONSTRAINT FK_PHONE_EMP_PH_ID FOREIGN KEY (EMP_PH_ID) REFERENCES EMPLOYEE (EMP_ID)")
[EL Fine]: Connection(876256661)--ALTER TABLE PHONE ADD CONSTRAINT FK_PHONE_EMP_PH_ID FOREIGN KEY (EMP_PH_ID) REFERENCES EMPLOYEE (EMP_ID)
[EL Finest]: Execute query DataModifyQuery(sql="ALTER TABLE EMPLOYEE ADD CONSTRAINT FK_EMPLOYEE_MANAGER_ID FOREIGN KEY (MANAGER_ID) REFERENCES EMPLOYEE (EMP_ID)")
[EL Fine]: Connection(876256661)--ALTER TABLE EMPLOYEE ADD CONSTRAINT FK_EMPLOYEE_MANAGER_ID FOREIGN KEY (MANAGER_ID) REFERENCES EMPLOYEE (EMP_ID)
[EL Finest]: Execute query DataModifyQuery(sql="ALTER TABLE EMPLOYEE ADD CONSTRAINT FK_EMPLOYEE_ADDR_ID FOREIGN KEY (ADDR_ID) REFERENCES ADDRESS (ADDRESS_ID)")
[EL Fine]: Connection(876256661)--ALTER TABLE EMPLOYEE ADD CONSTRAINT FK_EMPLOYEE_ADDR_ID FOREIGN KEY (ADDR_ID) REFERENCES ADDRESS (ADDRESS_ID)
[EL Finest]: Execute query DataModifyQuery(sql="ALTER TABLE PROJ_EMP ADD CONSTRAINT FK_PROJ_EMP_EMP_ID FOREIGN KEY (EMP_ID) REFERENCES EMPLOYEE (EMP_ID)")
[EL Fine]: Connection(876256661)--ALTER TABLE PROJ_EMP ADD CONSTRAINT FK_PROJ_EMP_EMP_ID FOREIGN KEY (EMP_ID) REFERENCES EMPLOYEE (EMP_ID)
[EL Finest]: Execute query DataModifyQuery(sql="ALTER TABLE PROJ_EMP ADD CONSTRAINT FK_PROJ_EMP_PROJ_ID FOREIGN KEY (PROJ_ID) REFERENCES PROJECT (PROJ_ID)")
[EL Fine]: Connection(876256661)--ALTER TABLE PROJ_EMP ADD CONSTRAINT FK_PROJ_EMP_PROJ_ID FOREIGN KEY (PROJ_ID) REFERENCES PROJECT (PROJ_ID)
[EL Finest]: Execute query DataModifyQuery(sql="ALTER TABLE PROJECT ADD CONSTRAINT FK_PROJECT_LEADER_ID FOREIGN KEY (LEADER_ID) REFERENCES EMPLOYEE (EMP_ID)")
[EL Fine]: 开发者_StackOverflowConnection(876256661)--ALTER TABLE PROJECT ADD CONSTRAINT FK_PROJECT_LEADER_ID FOREIGN KEY (LEADER_ID) REFERENCES EMPLOYEE (EMP_ID)
[EL Finest]: Execute query DataModifyQuery(sql="ALTER TABLE LPROJECT ADD CONSTRAINT FK_LPROJECT_PROJ_ID FOREIGN KEY (PROJ_ID) REFERENCES PROJECT (PROJ_ID)")
[EL Fine]: Connection(876256661)--ALTER TABLE LPROJECT ADD CONSTRAINT FK_LPROJECT_PROJ_ID FOREIGN KEY (PROJ_ID) REFERENCES PROJECT (PROJ_ID)
[EL Finest]: Execute query DataModifyQuery(sql="CREATE TABLE SEQUENCE (SEQ_NAME VARCHAR(50) NOT NULL, SEQ_COUNT DECIMAL(38), PRIMARY KEY (SEQ_NAME))")
[EL Fine]: Connection(876256661)--CREATE TABLE SEQUENCE (SEQ_NAME VARCHAR(50) NOT NULL, SEQ_COUNT DECIMAL(38), PRIMARY KEY (SEQ_NAME))
[EL Fine]: SELECT 1
The error you included is on project not phone?
Please include the DDL and SQL that is generated with logging on finest.
精彩评论