Java Spring Test DBUnit 和表模式名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22796147/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 17:57:57  来源:igfitidea点击:

Spring Test DBUnit and table schema name

javadbunitspring-test-dbunit

提问by perak

Is it possible to set the table schema name when using @DatabaseSetupannotation from Spring Test DBUnit? Currently I'm using it like this:

使用@DatabaseSetupSpring Test DBUnit 的注释时是否可以设置表模式名称?目前我是这样使用它的:

@DatabaseSetup("user-data.xml")
public class UserMapperTest {
}

user-data.xml: (I also tried to set the element name to user.system_user without any luck)

user-data.xml :(我也尝试将元素名称设置为 user.system_user 没有任何运气)

<dataset>
    <system_user
        ...
        />
</dataset>

And here I'm creating my table with schema called user:

在这里,我使用名为 user 的模式创建我的表:

create table "user".system_user (...);

And this is the exception that I get when running test:

这是我在运行测试时得到的异常:

org.h2.jdbc.JdbcSQLException: Table "SYSTEM_USER" not found; SQL statement:
delete from SYSTEM_USER [42102-175]

回答by Paizo

I had a similar issue. You cannot with the annotation however you can specify the schema on the connection string. Here is how I solved my case:

我有一个类似的问题。您不能使用注释,但是您可以在连接字符串上指定架构。这是我如何解决我的案例:

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="org.hsqldb.jdbcDriver" />
    <property name="url" value="jdbc:hsqldb:mem:fullywallet;MVCC=true" />
    <property name="username" value="sa" />
    <property name="password" value="" />
</bean>
<bean id="sessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
    <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan">
        <list>
            <value>your.package.to.be.scanned</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
            <prop key="hibernate.default_schema">public</prop>
        </props>
    </property>
</bean>

In my case I was using HSQLDB but it is applicable for other databases as well

就我而言,我使用的是 HSQLDB,但它也适用于其他数据库

回答by int21h

I used this trick. First, we need OracleConnection bean:

我用了这个技巧。首先,我们需要 OracleConnection bean:

<bean id="oracleConnection" class="org.dbunit.ext.oracle.OracleConnection">
    <constructor-arg value="#{dataSource.getConnection()}"/>
    <constructor-arg value="<your_scheme_name>"/>
</bean>

Then you can use this annotation in your methods

然后你可以在你的方法中使用这个注解

@DbUnitConfiguration(databaseConnection = "oracleConnection")
@DatabaseSetup(...)

Hope it helps.

希望能帮助到你。

回答by Gilberto

If you are using the spring-test-dbunitthen you need to create an IDatabaseConnection with a specific DBUnit configuration. Following the doc's example I've setup this one for me:

如果您使用的是spring-test-dbunit,那么您需要使用特定的 DBUnit 配置创建一个 IDatabaseConnection。按照文档的示例,我为我设置了这个:

<?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"

       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
">
    <bean id="dbUnitDatabaseConfig" class="com.github.springtestdbunit.bean.DatabaseConfigBean">
        <property name="datatypeFactory">
            <bean class="org.dbunit.ext.postgresql.PostgresqlDataTypeFactory" />
        </property>
        <!--property name="qualifiedTableNames" value="true" /-->
        <property name="caseSensitiveTableNames" value="true" />
    </bean>

    <bean id="dbUnitDatabaseConnection" class="com.github.springtestdbunit.bean.DatabaseDataSourceConnectionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="databaseConfig" ref="dbUnitDatabaseConfig"/>
        <property name="schema" value="sapeo"/>
    </bean>

</beans>