oracle 在连接上设置 H2 模式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29218921/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 02:47:18  来源:igfitidea点击:

Set H2 schema on connection

javaoraclehibernateh2

提问by Roger

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:

我正在为我们的 oracle 生产数据库配置一个 h2 测试数据库。所有表都属于架构xxx。我的数据源定义如下:

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]

有了SCHEMA=xxx,我得到一个错误: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.

没有SCHEMA=xxx,每当 Hibernate 尝试运行带有连接的查询时,我都会出错,因为它没有将架构添加到表名之前。尽管如此,它在我们的 oracle 数据库的生产中做到了这一点。

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;
}

回答by Roger

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");

此连接字符串有效: 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");

回答by Antoniossss

I suggest that you should use hibernate.hbl2ddl.auto=createto create scheme on connection.

我建议您应该使用hibernate.hbl2ddl.auto=create来创建连接方案。

Here some info Hibernate hbm2ddl.auto possible values and what they do?

这里有一些信息 Hibernate hbm2ddl.auto 可能的值以及它们的作用?