dbunit/unitils: how to export a multi-schema dataset?

981 views Asked by At

The official tutorial of dbunit already give a good example for exporting dataset from a single database schema.
Is there any way to export different tables from different schemas into one single dataset (say Table_A from schema_A, Table_B from schema_B)?
The exported dataset, when written into an xml file, would be like this:

<?xml version='1.0' encoding='UTF-8'?>
<dataset schema:schemaA schema:schemaB>
    <schemaA:tableA ..... />
    <schemaA:tableA ..... />
    <schemaB:tableB ..... />
</dataset>
1

There are 1 answers

0
Thiago Primerano On

I've just got the same problem and to fix it you need to set the FEATURE_QUALIFIED_TABLE_NAMES properties:

See below the same example code with the change (I removed some part of the code because I don't need full database export):

    public static void main(String[] args) throws Exception
    {
        // database connection
        Class driverClass = Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        Connection jdbcConnection = DriverManager.getConnection(
                "jdbc:sqlserver://<server>:1433;DatabaseName=<dbName>", "<usr>", "<passwd>");
        IDatabaseConnection connection = new DatabaseConnection(jdbcConnection);

        Properties props = new Properties();
        props.put(DatabaseConfig.FEATURE_QUALIFIED_TABLE_NAMES, "true");
        connection.getConfig().setPropertiesByString(props);        


        // dependent tables database export: export table X and all tables that
        // have a PK which is a FK on X, in the right order for insertion
        String[] depTableNames = TablesDependencyHelper.getAllDependentTables( connection, "vehicle.Vehicle_Series_Model_SMA" );
        IDataSet depDataset = connection.createDataSet( depTableNames );
        FlatXmlDataSet.write(depDataset, new FileOutputStream("vehicle.Vehicle_Series_Model_SMA.xml"));          

    }