0

我目前使用 spring 进行依赖注入。Hibernate 使用 postgres 方言进行正常运行,但我想将 HSQL 用于 DBUnitils 数据库测试。

我的Spring-Configuration包含以下内容:

<!-- 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>

这些字段被 Maven 资源过滤所取代。

DBUnitils 的 Spring-Configruation 包含以下内容:

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

因此使用 UnitilsDataSource 覆盖我的运行配置中的数据源。

问题:我无法使用 Postgres-Dialect 对 HSQL-Test-Database 运行测试,因为某些命令不起作用。 我想到的唯一解决方案:在 maven 中切换资源过滤器,但我必须手动执行此操作(通过在每个 maven 调用上证明“-P TEST”)。那么是否有可能覆盖hibernate.dialect?

谢谢!

4

2 回答 2

4

您通常根本不需要指定方言,Hibernate 会通过查看底层数据源来找出它。如果你想强制 Hibernate 使用特定的方言,你只需要指定方言。

在您的情况下,您应该能够完全删除 dialect 属性,并且它应该在 postgres 和 hsql 中工作而无需修改配置。

于 2009-09-16T13:02:33.060 回答
0

您可能应该考虑在春季使用 PropertyPlaceHolderConfigurer 来更改配置。这样你只需要为不同的环境提供不同的配置文件,spring xml 保持不变。

你可以像这样加载配置文件。

于 2009-09-16T17:00:07.613 回答