Spring 如何在mem HSQLDB序列中使用单元测试?

Spring 如何在mem HSQLDB序列中使用单元测试?,spring,junit,hsqldb,Spring,Junit,Hsqldb,我的应用程序使用序列,我正在尝试在mem数据库中使用HSQLDB设置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:conte

我的应用程序使用序列,我正在尝试在mem数据库中使用HSQLDB设置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>
导致

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

编辑:更改上述详细信息,以便使用正确的序列类。然而,它并没有解决这个问题。

您已经指定
org.springframework.jdbc.support.incrementer.HsqlMaxValueIncrementer
作为类,但是,Spring似乎正在使用
HsqlSequenceMaxValueIncrementer
(在同一个包中)并创建一个序列,而不是一个表


HsqlMaxValueIncrementer
是一个较旧的实现,适用于不支持序列对象的数据库平台。
HsqlSequenceMaxValueIncrementer
是较新的版本(Spring v.2.5),是HSQLDB更有效的选择。这将创建您指定的序列对象,然后在内部使用public.agents的下一个值来检索下一个序列值。

我遇到了一个类似的问题


提供了各种各样的解决方案。H2数据库似乎很有趣。最后,我手动删除,然后在脚本中创建序列。

接近结尾时,您会说“但尝试使用它会导致错误”。如何使用序列?如果您尝试为序列创建表,例如
createtableagents\u logs\u seq(value identity),会发生什么;在代理日志中插入顺序值(0)
@php coder因为我现在更改了sequence类,所以这应该是不必要的,对吧?哇,我从来没有想到过这一点。非常感谢。我改变了这一点,但现在我得到了“由以下原因引起的:java.sql.SQLException:Sequence已经存在于语句[CREATE Sequence public.agent_logs_seq]”中,当我注释掉它时,我得到“意外异常:org.springframework.dao.DataAccessResourceFailureException:无法获取序列值;嵌套异常为java.sql.SQLException:Sequence not found:AGENT_LOGS_SEQ in statement[call next value for public.AGENT_LOGS_SEQ]”。我也会更新这个问题。在@Before方法工作之前手动测试和创建。我真的不喜欢这样做,但必须接受,目前没有更好的选项:)
DataFieldMaxValueIncrementer incrementer =
    (DataFieldMaxValueIncrementer) context.getBean("logSeqIncrementer");
Integer logId = Integer.valueOf(incrementer.nextIntValue());