简体   繁体   English

Java:DBunitils + Spring:不同的Hibernate-Dialect

[英]Java: DBunitils + Spring: Different Hibernate-Dialect

I currently use spring for depency injection. 我目前使用spring进行脱脂注射。 Hibernate uses a postgres dialect for the normal run, but I want to use HSQL for the DBUnitils Databank-Testing. Hibernate使用postgres方言进行正常运行,但是我想将HSQL用于DBUnitils数据库测试。

My Spring-Configuration contains this: 我的春季配置包含以下内容:

<!-- Hibernate session factory -->
<bean id="sessionFactory"
      class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">${hibernate.dialect}</prop>
            <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
            <prop key="use_outer_join">${hibernate.use_outer_join}</prop>

            <prop key="hibernate.cache.use_second_level_cache">${hibernate.cache.use_second_level_cache}</prop>
            <prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_cache}</prop>
            <prop key="hibernate.cache.provider_class">${hibernate.cache.provider}</prop>

            <prop key="hibernate.connection.pool_size">10</prop>
            <prop key="hibernate.jdbc.batch_size">1000</prop>
            <prop key="hibernate.bytecode.use_reflection_optimizer">true</prop>

        </props>
    </property>
    <property name="annotatedClasses">
        <list>
            <value>de.dbruhn.relations.model.Relation</value>
            <value>de.dbruhn.relations.model.RelationData</value>
            <value>de.dbruhn.relations.model.RObject</value>
            <value>de.dbruhn.relations.model.Type</value>
       </list>
    </property>

    <property name="schemaUpdate" value="${hibernate.schemaUpdate}"/>
</bean>

The fields get replaced by maven resource-filtering. 字段被maven资源过滤替换。

The Spring-Configruation for DBUnitils contains this: 用于DBUnitils的Spring-Configruation包含以下内容:

<bean id="dataSource" class="org.unitils.database.UnitilsDataSourceFactoryBean"/>
</beans>

and so overrides the dataSource from my run configuration with the UnitilsDataSource. 因此,使用UnitilsDataSource从我的运行配置中覆盖了dataSource。

The Problem: I cant run the Tests using Postgres-Dialect against the HSQL-Test-Database because some commands dont work. 问题:由于某些命令不起作用,我无法对HSQL-Test-Database使用Postgres-Dialect运行测试。 The only solution which came to my mind: Switching the resource-filter in maven, but I have to do this by hand (by provining a "-P TEST" on every maven call). 我想到的唯一解决方案是:在Maven中切换资源过滤器,但我必须手动执行此操作(通过在每个Maven调用中提供“ -P TEST”)。 So isn't there a possibilty to override the hibernate.dialect? 因此,没有办法重写hibernate.dialect吗?

Thanks! 谢谢!

You normally don't need to specify the dialect at all, Hibernate will figure it out by looking at the underlying datasource. 通常您根本不需要指定方言,Hibernate会通过查看基础数据源来弄清楚它。 You only need to specify the dialect if you want to force Hibernate to use a specific one. 如果要强制Hibernate使用特定的方言,则只需指定方言。

In your case, you should just be able to remove the dialect property completely, and it should work in postgres and hsql without config modification. 在您的情况下,您应该只能够完全删除Dialect属性,它应该在postgres和hsql中工作,而无需修改配置。

You should possibly look at using the PropertyPlaceHolderConfigurer in spring to change the config. 您可能应该考虑在春季使用PropertyPlaceHolderConfigurer来更改配置。 That way you need only supply a different config file for the different environments, the spring xml stays the same. 这样,您只需要为不同的环境提供不同的配置文件,spring xml就保持不变。

And you can load the config file like this . 您可以像这样加载配置文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM