1

I have the following quite strange dataset for 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" />

the most strange thinks is persons table has foreign key to carwash however this column is nullable (therefore you cannot find carwash in person with id=1) and carwash table has FK to persons in column create_by

This scheme leads to special order for put data in DB. As I understand DBUnit doesn't put value in DB based on order the value mentioned in dataset by default. And consequently when this dataset is executing it lead to exception

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`))

Is any way to push DBUnit put data in DB in string order as it mentioned in xml ?

Bizon4ik
  • 2,604
  • 4
  • 20
  • 46

2 Answers2

1

In your dataset.xml file you must specify tables in correctly insertion order, this means, the basic tables first and then the related tables. This way and using DatabaseOperation.CLEAN_INSERT, tables will be deleted correctly too (related tables first and then basic tables).

Your xml file:

<?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>

Hope this helps.

  • But sometimes you can't order the tables because there are some FK dependencies and the ideal would be to respect dataset order instead of inserting all registers from each table. – lujop Apr 04 '22 at 10:10
0

Based on the error message, it is a SQL constraint violation from the database, not a dbUnit issue. Is Persons.carwash a nonnullable column, or requiring FK from Carwash? dbUnit cannot override database constraints, just as your own code cannot.

Jeff
  • 956
  • 8
  • 10
  • I don't want to override. I need that person with id =1 will be put in DB first than carwash should be put and only after that person id =2. By default DBUnit want to put all persons and only then carwash and it's lead to exception – Bizon4ik Feb 26 '17 at 20:28