简体   繁体   English

H2数据库是否与Oracle'Insert All'语句兼容?

[英]Is H2 database compatible with Oracle 'Insert All' statement?

I'm writing tests for MyBatis DAO layer for Oracle database and have a problem with batch insert operation. 我正在为Oracle数据库的MyBatis DAO层编写测试,并且批量插入操作有问题。 It is implemented with Oracle statement INSERT ALL : 它是通过Oracle语句INSERT ALL

<insert id="batchInsertElements" parameterType="java.util.List" useGeneratedKeys="false">
    INSERT ALL
    <foreach collection="list" item="element">
        INTO table1
        (
        col1, col2, col3, col4
        )
        VALUES
        (
        #{element.col1},
        (select nvl(c.ID, -1) from table2 c where c.name = #{element.col2}),
        #{element.col3},
        #{element.col4}
        )
    </foreach>
    SELECT 1 FROM DUAL
</insert>

Against real database, it is working with no errors. 针对真实数据库,它没有错误。 For tests, I'm using H2 database and spring-dbunit. 对于测试,我正在使用H2数据库和spring-dbunit。 Configuration for test database: 测试数据库的配置:

<bean id="testDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="org.h2.Driver" />
    <property name="url" value="jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;MODE=Oracle" />
    <property name="username" value="sa" />
    <property name="password" value="" />
</bean>

During a test for the batch insert I'm getting SQL syntax error: 在批量插入测试期间,我收到SQL语法错误:

org.springframework.jdbc.BadSqlGrammarException: 
### Error updating database.  Cause: org.h2.jdbc.JdbcSQLException: Syntax error in SQL statement "INSERT ALL[*]

        INTO TABLE1
        (
        COL1, COL2, COL3, COL4
        )
        VALUES
        (
        ?,
        (SELECT NVL(C.ID, -1) FROM TABLE2 C WHERE C.NAME = ?),
        ?,
        ?
        )

    SELECT 1 FROM DUAL "; expected "INTO";
 [42001-193]; bad SQL grammar [];

I'm suspecting that INSERT ALL statement is not supported by H2 database. 我怀疑H2数据库不支持INSERT ALL语句。 Is it so? 是这样吗? Any workaround? 任何解决方法? Can HSQLDB be more suitable for Oracle DAO testing? HSQLDB能否更适合Oracle DAO测试?

Unfortunately, neither H2 nor HSQLDB supports the Oracle proprietary INSERT ALL syntax. 不幸的是,H2和HSQLDB都不支持Oracle专有的INSERT ALL语法。 Short of having an instance of Oracle up and running for your unit/integration tests, I'm not sure there is another way around it. 除了没有一个Oracle实例可以运行并运行您的单元/集成测试外,我不确定还有其他方法可以解决。 You could look into using a dockerized Oracle XE container and spin it up as part of your test. 您可以考虑使用dockerized Oracle XE容器,并将其旋转作为测试的一部分。 Another option would be to get developers to look into moving away from the INSERT ALL . 另一种选择是让开发人员考虑脱离INSERT ALL

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

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