0

我有以下非常奇怪的 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但是这个列是可以为空的(因此你不能carwash亲自找到id=1)并且表在列carwash中有 FKpersonscreate_by

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

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 中提到的字符串顺序放入 DB 中?

4

2 回答 2

1

在您的 dataset.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>

希望这可以帮助。

于 2017-02-27T08:51:17.347 回答
0

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

于 2017-02-26T19:06:57.027 回答