Search code examples
oracle-databasedatabase-schemah2dbunitin-memory-database

dbunit schema issues with h2 and oracle, schema is always PUBLIC


From a previous export of an Oracle DB, i have both xml and dtd files with lots of data but dbunit seems to ignore the dtdfiles when i try to import the data.

flatXMLBuilder.build(xmlFile);

I always get this errormessage when trying to reference one object of a table:

ERROR Table 'TABLE' not found in tableMap=org.dbunit.dataset.OrderedTableNameMap

and the warning:

WARN  session=global o.d.database.DatabaseConnection - The given schema 'SA' does not exist

I am not sure if the including of the dtd files would fix the schemaproblem (which is always set to PUBLIC) But i would be happy if i at least could test it out. The files are always in the same directory:

T1.xml
T1.dtd

Also the core problem seems to be the schema which is always set to PUBLIC on my target H2 in memory db. I tried out some schema set methods like "set schema" or "init=create schema" in the connection url but the currentschema was always public.

So when i was logged in as the SA user for example i couldn't find any tables becaue in the SA schema are no tables only in PUBLIC.

I also tried the setSchema(String) method on the h2connection but it don't work (i get an uncatchable error on call)

Update: Currently i use a FileInputStream to read the dtd file and add it to the builder:

builder.setMetaDataSetFromDtd(dtdStream);

But it didn't help with the problem.


Solution

  • If you want to create a schema x, and also set the default schema to x, in the database URL, then you need to use:

    jdbc:h2:~/data/databaseName;init=create schema if not exists x\;set schema x
    

    Please note the escaped semicolon. In Java, you need to escape the backslash as well:

    String url = "jdbc:h2:~/data/databaseName;init=create schema if not exists x\\;set schema x";
    

    This URL is a bit long. You might want to move all the statements to an init script, and just run that:

    jdbc:h2:~/data/databaseName;init=runscript 'init.sql'