Derby 上的 Oracle 序列 nextval 问题
Oracle sequence nextval issue on Derby
我们在 Oracle 数据库之上的应用程序中有多个 MyBatis 查询,它们使用序列生成自己的 ID 值以供进一步使用
<insert id=...>
<selectKey keyColumn="some_id" keyProperty="someId" order="BEFORE" resultType="int">
select s_seq1.nextval from dual
</selectKey>
insert into some_table
(some_id, some_data, created_on, created_by)
values
(#{someId}, #{someData}, #{createdOn}, #{createdBy})
</insert>
我们现在正尝试使用 Derby 数据库为应用程序编写集成测试,但由于它没有 dual table,我们试图通过创建一个名为 dual 的假视图来使上述查询工作,其中包含序列 nextval 表达式作为其列。带有两个示例列的 Liquibase 变更集如下
<changeSet id="create_dual_view">
<sql><![CDATA[create view dual as select 1 "s_seq1.nextval", 2 "s_seq2.nextval" from SYSIBM.SYSDUMMY1;]]></sql>
</changeSet>
检查 Derby 内容,此视图已成功创建,但是 运行 测试时我们仍然收到上述查询的异常
java.sql.SQLSyntaxErrorException: Column 'S_SEQ1.NEXTVAL' is either not in any table in the FROM list or appears within a join specification
and is outside the scope of the join specification or appears in a HAVING clause and is not in the GROUP BY list.
问题似乎是由于点我们不得不在引号之间创建假的视图列,所以 MyBatis 查询也可以在 Derby 上使用引号之间的列名工作,但这将使真正的应用程序在 Oracle 上失败。这似乎是一个僵局。任何想法将不胜感激。
一种解决方案是根据底层数据库执行不同的查询。参见 http://www.mybatis.org/mybatis-3/dynamic-sql.html ("Multi-db vendor support" section) and http://www.mybatis.org/mybatis-3/configuration.html#databaseIdProvider。
代码应如下所示:
<insert id=...>
<selectKey keyColumn="some_id" keyProperty="someId" order="BEFORE" resultType="int">
<if test="_databaseId == 'oracle'">
select s_seq1.nextval from dual
</if>
<if test="_databaseId == 'derby'">
[...]
</if>
</selectKey>
insert into some_table
(some_id, some_data, created_on, created_by)
values
(#{someId}, #{someData}, #{createdOn}, #{createdBy})
</insert>
我们在 Oracle 数据库之上的应用程序中有多个 MyBatis 查询,它们使用序列生成自己的 ID 值以供进一步使用
<insert id=...>
<selectKey keyColumn="some_id" keyProperty="someId" order="BEFORE" resultType="int">
select s_seq1.nextval from dual
</selectKey>
insert into some_table
(some_id, some_data, created_on, created_by)
values
(#{someId}, #{someData}, #{createdOn}, #{createdBy})
</insert>
我们现在正尝试使用 Derby 数据库为应用程序编写集成测试,但由于它没有 dual table,我们试图通过创建一个名为 dual 的假视图来使上述查询工作,其中包含序列 nextval 表达式作为其列。带有两个示例列的 Liquibase 变更集如下
<changeSet id="create_dual_view">
<sql><![CDATA[create view dual as select 1 "s_seq1.nextval", 2 "s_seq2.nextval" from SYSIBM.SYSDUMMY1;]]></sql>
</changeSet>
检查 Derby 内容,此视图已成功创建,但是 运行 测试时我们仍然收到上述查询的异常
java.sql.SQLSyntaxErrorException: Column 'S_SEQ1.NEXTVAL' is either not in any table in the FROM list or appears within a join specification
and is outside the scope of the join specification or appears in a HAVING clause and is not in the GROUP BY list.
问题似乎是由于点我们不得不在引号之间创建假的视图列,所以 MyBatis 查询也可以在 Derby 上使用引号之间的列名工作,但这将使真正的应用程序在 Oracle 上失败。这似乎是一个僵局。任何想法将不胜感激。
一种解决方案是根据底层数据库执行不同的查询。参见 http://www.mybatis.org/mybatis-3/dynamic-sql.html ("Multi-db vendor support" section) and http://www.mybatis.org/mybatis-3/configuration.html#databaseIdProvider。
代码应如下所示:
<insert id=...>
<selectKey keyColumn="some_id" keyProperty="someId" order="BEFORE" resultType="int">
<if test="_databaseId == 'oracle'">
select s_seq1.nextval from dual
</if>
<if test="_databaseId == 'derby'">
[...]
</if>
</selectKey>
insert into some_table
(some_id, some_data, created_on, created_by)
values
(#{someId}, #{someData}, #{createdOn}, #{createdBy})
</insert>