最近在做应用服务器的CTS测试时发现这样一个问题,跑测试用例过程中,许多用例会提示“
[<openjpa-2.2.0-r422266:1244990 fatal store error> org.apache.openjpa.util.ObjectExistsException: Cannot add or update a child row: a foreign key constraint fails ”
仔细看了下用例的代码,发现出错的地方是数据库中的表存在外键,而在用例的代码中并没有先持久化外键关联的数据,而是直接持久化包含关联的数据导致问题的。说的有点绕,举个例子:例如User这个对象引用了Address这个对象,并且Address是做为外键在User中被引用的,此时,如果持久化数据时没有先持久化Address而是直接持久化User,就会导致此问题,而如果先持久化了Address,问题则不存在。
由于CTS是Oracle官方标准的用例,是不能通过修改用例的方式通过测试的,因此只能分析JPA的实现本身,我们的应用服务器中使用的是OpenJPA的实现,google发现类似的问题不少,但可以通过在JPA的配置文件persistence.xml中增加
<property name="openjpa.jdbc.MappingDefaults"
value="ForeignKeyDeleteAction=restrict, JoinForeignKeyDeleteAction=restrict"/>
来强制进行级联插入的时候先插入外键的表,再插入主表。
这样对于自己的应用可以,但如上面所说,并不能通过修改用例来解决问题,更何况用例数量很多。因此,通过修改OpenJPA的源码来使其默认的ForeignKey就是restrict来解决此问题。
修改位置:
openjpa-jdbc.jar
org.apache.openjpa.jdbc.meta.MappingDefaultsImpl的
62和63行,将默认没有ForeignKey配置的代码
private int _joinFKAction = ForeignKey.ACTION_NONE;
private int _fkAction = ForeignKey.ACTION_NONE;
改为:
private int _joinFKAction = ForeignKey.ACTION_RESTRICT;
private int _fkAction = ForeignKey.ACTION_RESTRICT;
ref:
https://issues.apache.org/jira/browse/OPENJPA-435
https://openjpa.apache.org/faq.html
http://hi.baidu.com/justmeyalan/item/829a7101e1f5e911ebfe3807
分享到:
相关推荐
(1452, 'Cannot add or update a child row: a foreign key constraint fails (`mxonline`.`django_admin_log`, CONSTRAINT `django_admin_log_user_id_c564eba6_fk_auth_user_id` FOREIGN KEY (`user_id`) ...
ERROR 1217 (23000): Cannot delete or update a parent row: a foreign key constraint fails 这是因为你尝试删除的表中的字段被用作了其他表的外键,因此在删除这个表(父表)之前必须先删除具有外键的表(子表)...
这篇文章主要涉及到在数据创建表时,遇到ERROR 1215 (HY000): Cannot add foreign key constraint 问题方面的内容,对于在数据创建表时,遇到同样问题感兴趣的同学可以参考一下。 一、问题的提出 创建两个表: ...
在SSM项目中执行一个删除用户操作时报错,遂在navicat中尝试是否可以直接删除,报如下所示错误 student表的主键是selectedcourse表的外键,当需要删除student表内的一行数据时,必须在selectedcourse表内设置该外键...
Cannot delete or update a parent row: a foreign key constraint fails (...) 这可能是MySQL在InnoDB中设置了foreign key关联,造成无法更新或删除数据。可以通过设置FOREIGN_KEY_CHECKS变量来避免这种情况。 SET ...
aforeignkeyconstraintfails (...) 这是因为MySQL中设置了foreign key关联,造成无法更新或删除数据。可以通过设置FOREIGN_KEY_CHECKS变量来避免这种情况。 禁用外键约束,我们可以使用: SETFOREIGN_KEY_CHECKS=...
阅读此书以了解根本原因:SQL FOREIGN KEY Constraint [^]
The foreign key in a table T1 _____ the same _____ as the corresponding primary key in table T2. must have, name need not have, name must have, domain (a) I, II, and III (b) I and II (c) ...
在MySQL中,InnoDB引擎类型的表支持了外键约束。外键的使用条件:1.两个表必须是InnoDB表,MyISAM表暂时不支持外键(据说以后的版本有可能支持,但...外键的定义语法: 代码如下:[CONSTRAINT symbol] FOREIGN KEY [i
Grammatical theory From transformational grammar to constraint-based approaches Stefan Müller 2017新书,欢迎对形式语言学,自然语言处理等领域感兴趣读者下载。
"SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'a3b6420a-6724-11ea-b2a3-d773d1d6999f' for key 'callId'\nThe SQL being executed was: INSERT INTO `ly_call` (`call_id`, `mobile`, ...
Processing Rule : Short-Circuit Constraint (Allowed=Not Allowed) (On the board ),(On the board ) Rule Violations :0 Processing Rule : Broken-Net Constraint ( (On the board ) ) Rule Violations :0 ...
课程设计—仓库设备管理系统.doc 创建基本表的SQL代码: create table Equipment ( ...alter table Stock add constraint Eno3 foreign key(Eno) references Equipment(Eno) on update cascade on delete cascade
constraint 外键名 foreign key(字段) references 主表(字段) on delete 级联动作 on update 级联动作 3、级联动作 1、cascade :删除、更新同步(被参考字段) 2、restrict :不让主表更新、删除 3、set null...
SQL Server 2000中的列级约束与表级约束 在SQL Server 2000中有5 种约束: 主键约束(primary key constraint) 唯一性约束(unique constraint) 检查约束(check constraint...外部键约束(foreign key constraint)
Coarse grained Hostility Detection: The task is to predict whether a given post is hostile or not. ii. Fine-grained Hostility Detection: The task is to detect one or more hostile dimensi
This two-volume book shows how to design and implement web applications with a model-based engineering approach, using the two most relevant technology plat- forms for web development: JavaScript and ...
gruopid int CONSTRAINT g_FORE FOREIGN KEY REFERENCES user_group(groupid), ) CREATE TABLE chat_log ( logid int CONSTRAINT l_PRIM PRIMARY KEY, senderid int CONSTRAINT s_FORE FOREIGN KEY REFERENCES ...
foreign key(AID) references dbo.A(AID) on delete cascade on update cascade go alter table dbo.C add constraint FK_C_B_BID foreign key(BID) references dbo.B(BID) on delete cascade on update cascade go ...