我在MySQL 5.5.9遇到1452錯誤,同時運行,我脫下了網站的EclipseLink JPA的一些教程代碼的關係( http://wiki.eclipse.org/EclipseLink/Examples/JPA/EmployeeXML),我希望有人有一些洞察力。外鍵約束問題涉及一個一對多和多對一一個在EcliplseLink2.1.2
我使用的EclipseLink最新的穩定版本今天從主網站: 的EclipseLink - JPA-modelgen_2.1.2.v20101206-r8635.jar和javax.persistence_2.0.1.v201006031150.jar。
當我通過CreateDatabase.java填充數據庫,我收到以下錯誤:
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:不能添加或更新子行,外鍵約束失敗( 。jpatutorial
phone
,約束FK_PHONE_EMP_PH_ID
外鍵(EMP_PH_ID
)參考文獻EMPLOYEE
(EMP_ID
)) 錯誤代碼:1452 呼叫:????INSERT INTO PHONE(PH_ID,TYPE,AREA_CODE,P_NUMBER,EMP_PH_ID)VALUES(,,,,? ) bind => [25,Work,613,5558812,23] Query:InsertObjectQuery(Phon e([Work] 613 - 5558812))
這裏的相關模型是Employee表和Phone表。電話與員工之間存在多對一的關係。在orm.xml中的一部分是低於:
<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>
代碼成功創建表,爲員工創造地址記錄,創建所有員工記錄,但隨後未能在插入電話記錄,即使它有一個有效的FK參考員工記錄。我簡化了代碼,以便它執行,保存並提交所有內容直至失敗,但是,無論是通過JPA執行插入操作還是通過運行SQL腳本手動執行操作,最終都會出現相同的1452錯誤。有趣的是,我發現如果我刪除了所有相關的Employee實體,然後重新創建它們以創建由MySQL工作臺反向設計的表定義,我可以通過JPA成功地將記錄插入到所有表中,並通過腳本手動執行。所以,這表明Eclipselink正在以某種方式創建表,導致產生問題,並且MySQL Workbench沒有在逆向工程中捕獲這些有問題的定義。在這一點上,我沒有想法。
從登錄FINEST追加DDL:
[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]: Connection(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
我在FINEST上添加了表創建日誌。該定義看起來與MySQL工作臺產生的相同。 – user635699 2011-03-01 05:48:41
我忘記了我刪除了代碼中插入的電話對象。我將它們添加回來,並粘貼了由此產生的錯誤。 – user635699 2011-03-01 05:53:45
請格式化日誌以使其可讀,還包括所有插入到錯誤的SQL日誌。 – James 2011-03-01 14:08:34