DBUnit order for dataset



我有以下非常奇怪的DBUnit数据集:

<persons id = "1"
first_name ="TestName99"
second_name = "TestSecondName"
father_name = "TestFatherName"
phone_number = "123456789"
date_of_birth = "1985-12-12 00:16:14"
role = "ROLE_OWNER"
date_of_creation = "2016-09-23 23:09:28"
enable = "1" />
<carwash id = "1"
name = "TestCarWash"
address = "test car wash address "
phone_number = " 123456789"
box_count = "5"
first_shift = "08:00:00"
second_shift = "20:00:00"
created_by = "1"
date_of_creation = "2016-09-23 23:09:28"
enable = "1" />
<persons id = "2"
first_name ="TestName100"
second_name = "TestSecondName"
father_name = "TestFatherName"
phone_number = "123456789"
date_of_birth = "1985-12-12 00:16:14"
role = "ROLE_WASHERMAN"
date_of_creation = "2016-09-23 23:09:28"
carwash = "1"
enable = "1" />

最奇怪的想法是persons表有外键要carwash但是此列可为空(因此您无法找到carwashid=1),并且carwash表具有 FK 以personscreate_by

此方案导致在数据库中放置数据的特殊顺序。据我了解,默认情况下,DBUnit 不会根据数据集中提到的值的顺序将值放入数据库中。因此,当此数据集执行时,它会导致异常

MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`test_pitstop`.`persons`, CONSTRAINT `persons_ibfk_1` FOREIGN KEY (`carwash`) REFERENCES `carwash` (`id`))

有没有办法推送DBUnit将数据按xml中提到的字符串顺序放入数据库中?

在数据集.xml文件中,您必须以正确的插入顺序指定表,这意味着首先指定基本表,然后指定相关表。这样并使用DatabaseOperation.CLEAN_INSERT,表也将正确删除(首先是相关表,然后是基本表)。

您的 xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<dataset>
<carwash id = "1"
name = "TestCarWash"
address = "test car wash address "
phone_number = " 123456789"
box_count = "5"
first_shift = "08:00:00"
second_shift = "20:00:00"
created_by = "1"
date_of_creation = "2016-09-23 23:09:28"
enable = "1" />
<persons id = "1"
first_name ="TestName99"
second_name = "TestSecondName"
father_name = "TestFatherName"
phone_number = "123456789"
date_of_birth = "1985-12-12 00:16:14"
role = "ROLE_OWNER"
date_of_creation = "2016-09-23 23:09:28"
enable = "1" />
<persons id = "2"
first_name ="TestName100"
second_name = "TestSecondName"
father_name = "TestFatherName"
phone_number = "123456789"
date_of_birth = "1985-12-12 00:16:14"
role = "ROLE_WASHERMAN"
date_of_creation = "2016-09-23 23:09:28"
carwash = "1"
enable = "1" />
</dataset>

希望这有帮助。

根据错误消息,这是来自数据库的 SQL 约束冲突,而不是 dbUnit 问题。 Persons.carwash是不可为空的列,还是需要洗车的FK?dbUnit 不能覆盖数据库约束,就像您自己的代码不能一样。

相关内容

  • 没有找到相关文章

最新更新