I am configuring an h2 test database for our oracle production database. All tables are of the schema xxx
. My datasource is defined as below:
public DataSource dataSource() {
JdbcDataSource ds = new JdbcDataSource();
ds.setUrl("jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;MODE=Oracle;TRACE_LEVEL_SYSTEM_OUT=2;INIT=CREATE SCHEMA IF NOT EXISTS xxx;SCHEMA=xxx");
ds.setUser("xxx");
ds.setPassword("xxx");
return ds;
}
With SCHEMA=xxx
, I get an error: Caused by: org.h2.jdbc.JdbcSQLException: Schema "xxx" not found; SQL statement: SET SCHEMA xxx [90079-186]
Without SCHEMA=xxx
, I get errors whenever Hibernate attempts to run a query with a join because it does not prepend the schema to the table name. It does this in production with our oracle database though.
Edit: To provide some more insight, I am populating my db from creation scripts that are also used in production:
@Bean
public DataSourceInitializer dataSourceInitializer(final DataSource dataSource) {
final DataSourceInitializer initializer = new DataSourceInitializer();
initializer.setDataSource(dataSource);
initializer.setDatabasePopulator(databasePopulator());
return initializer;
}
private DatabasePopulator databasePopulator() {
final ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
populator.setSeparator(";");
populator.setCommentPrefix("--");
populator.addScript(new ClassPathResource("db-schema.sql"));
populator.addScript(new ClassPathResource("db-init-data.sql"));
return populator;
}
There is a setDefaultSchemaName() method on the Database object that you can use to set a different default schema.
Example. In this example, let us create a schema named test_schema under SA user, using the following command. CREATE SCHEMA test_schema AUTHORIZATION sa; The above command produces the following output.
This connection string worked: ds.setUrl("jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;MODE=Oracle;TRACE_LEVEL_SYSTEM_OUT=2;INIT=CREATE SCHEMA IF NOT EXISTS xxx\\;SET SCHEMA xxx");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With