Spring + Hibernate + Hsqldb单元测试 - java.sql.SQLSyntaxErrorException:主键已经存在

时间:2015-06-30 10:28:42

标签: java spring hibernate maven hsqldb

我正在尝试将测试套件添加到现有的Java maven项目中。 我想创建一个内存数据库来执行测试。

这里是文件:

的pom.xml

        <dependency>
            <groupId>org.hsqldb</groupId>
            <artifactId>hsqldb</artifactId>
            <version>2.3.2</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>3.5.6-Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>3.5.6-Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.0-api</artifactId>
            <version>1.0.1.Final</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.6.6</version>
        </dependency>


        <dependency>
                <groupId>org.dbunit</groupId>
                <artifactId>dbunit</artifactId>
                <version>2.5.0</version>
                <type>jar</type>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>com.github.springtestdbunit</groupId>
                <artifactId>spring-test-dbunit</artifactId>
                <version>1.2.0</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-jdbc</artifactId>
                <version>3.2.3.RELEASE</version>
            </dependency>
            <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>3.2.3.RELEASE</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-dbcp2</artifactId>
            <version>2.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
            <version>2.4.1</version>
        </dependency>


        <!-- fine dipendenze per database di test -->

的hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory name="sessionFactory">
        <property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
        <property name="hibernate.current_session_context_class">thread</property>

        <mapping class="it.XXX.jpa.Aggregation" />
        <mapping class="it.XXX.jpa.AllocationUdcOperation" />
        /** ... */
        <mapping class="it.XXX.jpa.Zone" />

    </session-factory>
</hibernate-configuration>

弹簧db.xml

<?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:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!-- Define the session factory parameters for Hibernate -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="useTransactionAwareDataSource" value="false" />
        <property name="dataSource" ref="dataSource" />
        <property name="schemaUpdate" value="true" />
        <property name="configLocation" value="classpath:standard/hibernate.cfg.xml" />
        <property name="namingStrategy">
            <bean class="org.hibernate.cfg.DefaultComponentSafeNamingStrategy" />
        </property>
        <property name="eventListeners">
            <map>
                <entry key="merge">
                    <bean class="org.springframework.orm.hibernate3.support.IdTransferringMergeEventListener" />
                </entry>
            </map>
        </property>
        <property name="hibernateProperties">
            <map>
                <entry key="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect" />
<!--                <entry key="hibernate.show_sql" value="true" /> -->
            </map>
        </property>
    </bean>

    <!-- Define that we use the Hibernate transaction manager -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <!-- DataSource to use for dbimport -->
    <bean id="importds" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
        <property name="driverClass" value="net.sourceforge.jtds.jdbc.Driver" />
        <property name="jdbcUrl" value="jdbc:jtds:sqlserver://XXXXXXX:XXX;SelectMethod=cursor;sendStringParametersAsUnicode=false;databasename=YYYYY" />
        <property name="user" value="XXXXX" />
        <property name="password" value="XXXXXX" />
        <property name="minPoolSize" value="30" />
        <property name="maxPoolSize" value="100" />
        <property name="checkoutTimeout" value="60" />
        <property name="maxStatements" value="500" />
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager" />

    <!-- ============================================================== WEB INTERCEPTORS =============================================================== -->
    <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="interceptors">
            <list>
                <ref bean="openSessionInViewInterceptor" />
            </list>
        </property>
    </bean>
    <bean name="openSessionInViewInterceptor" class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor">
        <property name="sessionFactory">
            <ref bean="sessionFactory" />
        </property>
    </bean>

    <beans profile="production">
        <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
            <property name="driverClass" value="net.sourceforge.jtds.jdbc.Driver" />
            <property name="jdbcUrl" value="jdbc:jtds:sqlserver://xxxxxxxx:xxxx;SelectMethod=cursor;sendStringParametersAsUnicode=false;databasename=xxxxxx" />
            <property name="user" value="xxxxxx" />
            <property name="password" value="xxxxxx" />
            <property name="initialPoolSize" value="0" />
            <property name="minPoolSize" value="0" />
            <property name="maxIdleTime" value="600" />
            <property name="maxPoolSize" value="100" />
            <property name="checkoutTimeout" value="2000" />
            <property name="maxStatements" value="0" />
        </bean>
    </beans>

<!--    <beans profile="development"> -->
<!--        <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> -->
<!--            <property name="driverClass" value="net.sourceforge.jtds.jdbc.Driver" /> -->
<!--            <property name="jdbcUrl" value="jdbc:jtds:sqlserver://xxxxxx:xxxx;SelectMethod=cursor;sendStringParametersAsUnicode=false;databasename=xxxx" /> -->
<!--            <property name="user" value="xxxxx" /> -->
<!--            <property name="password" value="XXXXX" /> -->
<!--            <property name="initialPoolSize" value="0" /> -->
<!--            <property name="minPoolSize" value="0" /> -->
<!--            <property name="maxIdleTime" value="600" /> -->
<!--            <property name="maxPoolSize" value="100" /> -->
<!--            <property name="checkoutTimeout" value="2000" /> -->
<!--            <property name="maxStatements" value="0" /> -->
<!--        </bean> -->
<!--    </beans> -->

     <beans profile="development">
        <!-- <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
            <property name="driverClass" value="net.sourceforge.jtds.jdbc.Driver" />
            <property name="jdbcUrl" value="jdbc:jtds:sqlserver://XXXXXXXX:XXXX;SelectMethod=cursor;sendStringParametersAsUnicode=false;databasename=ZZZZZZZZ" />
            <property name="user" value="XXXX" />
            <property name="password" value="XXXX" />
            <property name="minPoolSize" value="30" />
            <property name="maxPoolSize" value="200" />
            <property name="checkoutTimeout" value="120" />
            <property name="maxStatements" value="0" />
        </bean>
         -->
        <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" 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>
    </beans>



</beans>

TestDb2.java

package it.vitalegi.db;

import org.junit.runner.RunWith;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;

@RunWith(SpringJUnit4ClassRunner.class)
@ActiveProfiles("development")
@ContextConfiguration({ "classpath:/it/XXXX/standard/spring-bootstrap.xml" })
//, "classpath:/hibernate.cfg.test.xml" })
//@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = false)
@Transactional
public class TestDb2 {
}

Test1.java

package it.vitalegi.testing;

import it.XXX.business.AreaService;
import it.XXX.business.ServiceLocator;
import it.XXX.business.UserService;
import it.XXX.jpa.Area;
import it.XXX.jpa.User;
import it.vitalegi.db.TestDb2;

import org.junit.Test;

public class Test1 extends TestDb2 {

    @Test
    public void test1() throws Exception {
        System.out.println("Test - AAA");
        final UserService u = ServiceLocator.getInstance().getUserService();
        System.out.println("Test - 1");
        final User user = u.createAdministratorFromData("AAA", "1");
        System.out.println("Test - 2");

        System.out.println("Test - 3");

        //final Context context = Context.createFromUser(user);
        //System.out.println("Test - 3");
        final AreaService areaService = ServiceLocator.getInstance().getAreaService();

        System.out.println("Test - 4");
        final Area res = areaService.findByCode("B04");
        System.out.println(res.getCode() + res.getDescription());
        System.out.println("fine test - AAA");
    }
}

在Test1.java上运行测试,我获得:

http://pastebin.com/FTT2iDn0

同时,将spring-db.xml代码更改为(因此使用真实数据库运行):

     <beans profile="development">
        <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
            <property name="driverClass" value="net.sourceforge.jtds.jdbc.Driver" />
            <property name="jdbcUrl" value="jdbc:jtds:sqlserver://XXXXXXXX:XXXX;SelectMethod=cursor;sendStringParametersAsUnicode=false;databasename=ZZZZZZZZ" />
            <property name="user" value="XXXX" />
            <property name="password" value="XXXX" />
            <property name="minPoolSize" value="30" />
            <property name="maxPoolSize" value="200" />
            <property name="checkoutTimeout" value="120" />
            <property name="maxStatements" value="0" />
        </bean>
         <!--<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" 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>-->
    </beans>

并执行与上述相同的测试,它可以工作:

http://pastebin.com/FkVqmezs

任何我想念的地方?感谢

0 个答案:

没有答案