Bizon4ik
Bizon4ik

Reputation: 2764

DBUnit order for dataset

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 ?

Upvotes: 1

Views: 1711

Answers (2)

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.

Upvotes: 1

Jeff
Jeff

Reputation: 953

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.

Upvotes: 0

Related Questions