2017-02-26 103 views
0

我对DBUnit的下面很奇怪的数据集: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提到?

回答

0

根据错误消息,它违反了数据库的SQL约束,而不是dbUnit问题。 Persons.carwash是一个不可空的列,或者需要Carwash的FK吗? dbUnit不能覆盖数据库约束,就像你自己的代码不能。

+0

我不想重写。我需要id = 1的人先放在DB中,而不是应该放在洗衣机中,并且只能在该人id = 2之后放入。默认情况下,DBUnit希望放置所有人,只有在洗车后才会导致异常 –

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> 

希望这有助于。