简体   繁体   English

SimpleJdbcInsert-不以默认模式在表中插入数据

[英]SimpleJdbcInsert - Not inserting data in tables in default schema

I am struggling with a code that uses Spring's SimpleJdbcInsert. 我在努力使用Spring的SimpleJdbcInsert的代码。

SimpleJdbcInsert jdbcInsert = (new SimpleJdbcInsert(transactionManager.getDataSource())
                .withTableName(MY_TABLE).usingGeneratedKeyColumns("my_table_id")).usingColumns(MY_COLUMNS);

This works fine when I use it on a connection that has the default schema as "PUBLIC", as in where I do not have any schemas set up. 当我在默认模式为“ PUBLIC”的连接上使用它时,这种方法可以很好地工作,因为我没有设置任何模式。 However, when I add a few schemas, and then make MY_SCHEMA1 as the default schema for the connection, this does not work any more. 但是,当我添加一些架构,然后将MY_SCHEMA1设置为连接的默认架构时,这将不再起作用。 It does not find the table MY_TABLE at all. 它根本找不到表MY_TABLE。

It starts working if I add .withSchemaName(MY_SCHEMA1) to SimpleJdbcInsert. 如果将.withSchemaName(MY_SCHEMA1)添加到SimpleJdbcInsert,它将开始工作。 However, I do not have that option. 但是,我没有那个选择。

Please help. 请帮忙。

Note: I am using this on H2 and DB2. 注意:我正在H2和DB2上使用它。

As far as I know, whenever a single DB is divided into multiple schemas, you have to specifically specify the schema you are pointing to(irrespective of the default one). 据我所知,每当一个数据库分为多个架构时,就必须专门指定要指向的架构(与默认架构无关)。 I think, that is the reason it works fine when you use add .withSchemaName(MY_SCHEMA1). 我认为,这就是当您使用add .withSchemaName(MY_SCHEMA1)时它可以正常工作的原因。 However, as you have mentioned you don not have this option, you can specify the schema name somewhere else, like in server config files, your application's properties file. 但是,如前所述,您没有此选项,您可以在其他位置指定架构名称,例如在服务器配置文件中,即应用程序的属性文件。

Dynamically invoking methods like withSchemaName() and useColumns() at arbitrary times is the main claim to usefulness of "query builder" classes like SimpleJdbcInsert: why are you using it for a constant? 在任意时间动态调用诸如withSchemaName()和useColumns()之类的方法是对诸如“ SimpleJdbcInsert”之类的“查询生成器”类有用的主要主张:为什么要使用它作为常量?

If you insist on a final SimpleJdbcInsert variable, it can be a generic one (written like in the code example, without a schema) and you can then call withSchemaName() on it, after you find out which schema you want to use, to get a completely specified SimpleJdbcInsert you can execute. 如果坚持使用最终的SimpleJdbcInsert变量,则它可以是一个通用变量(如代码示例中那样编写,没有模式),然后在找到要使用的模式后,可以对它调用withSchemaName()来获得可以执行的完全指定的SimpleJdbcInsert。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM