我的应用程序使用序列,我正在尝试使用具有类似设置的 HSQLDB in-mem 数据库设置 junit 测试环境。这就是我配置序列及其创建的方式:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" lazy-init="true"
destroy-method="close">
<property name="driverClassName" value="org.hsqldb.jdbcDriver" />
<property name="url" value="jdbc:hsqldb:mem:test"/>
<property name="username" value="sa" />
<property name="password" value="" />
</bean>
<bean id="logSeqIncrementer" class="org.springframework.jdbc.support.incrementer.HsqlSequenceMaxValueIncrementer">
<property name="dataSource" ref="dataSource" />
<property name="incrementerName" value="public.agent_logs_seq" />
</bean>
<bean id="offerSeqIncrementer" class="org.springframework.jdbc.support.incrementer.HsqlSequenceMaxValueIncrementer">
<property name="dataSource" ref="dataSource" />
<property name="incrementerName" value="public.offers_seq" />
</bean>
<jdbc:embedded-database
id="test"
type="HSQL">
<jdbc:script
location="classpath:/create-ddl.sql" />
</jdbc:embedded-database>
</beans>
以及create-ddl.sql的内容:
CREATE SEQUENCE public.agent_logs_seq;
结果是
java.sql.SQLException: Sequence already exists in statement
但如果我把它评论出来,我会得到
java.lang.AssertionError: Unexpected exception:
org.springframework.dao.DataAccessResourceFailureException:
Could not obtain sequence value; nested exception is
java.sql.SQLException: Sequence not found:
AGENT_LOGS_SEQ in statement [call next value for public.agent_logs_seq]
我尝试使用序列的方式:
DataFieldMaxValueIncrementer incrementer =
(DataFieldMaxValueIncrementer) context.getBean("logSeqIncrementer");
Integer logId = Integer.valueOf(incrementer.nextIntValue());
编辑:更改了上面的细节,以便使用正确的序列类。然而,它并没有解决问题。