3

私のアプリケーションはシーケンスを使用しており、同様のセットアップを持つ 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>

そして 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()); 

編集:正しいシーケンスクラスが使用されるように上記の詳細を変更しました。ただし、問題は解決しませんでした。

4

2 に答える 2

3

クラスとして指定org.springframework.jdbc.support.incrementer.HsqlMaxValueIncrementerしましたが、SpringがHsqlSequenceMaxValueIncrementer(同じパッケージで)使用しているようで、TABLEではなくSEQUENCEを作成しています。

HsqlMaxValueIncrementer、SEQUENCE オブジェクトをサポートしないデータベース プラットフォームに適した古い実装です。これHsqlSequenceMaxValueIncrementerはより最近のもの (Spring v. 2.5) であり、HSQLDB のより効率的な選択肢です。これにより、指定した SEQUENCE オブジェクトが作成NEXT VALUE FOR public.agents_logs_seqされ、次のシーケンス値を取得するために内部的に使用されます。

于 2012-08-19T21:41:35.043 に答える
1

私はここで提起された同様の問題を抱えていました。

さまざまなソリューションが提供されました。H2データベースは面白そうだった。最後に、手動でドロップしてから、スクリプトでシーケンスを作成しました。

于 2012-08-25T08:54:05.230 に答える