【问题标题】:Java: DBunitils + Spring: Different Hibernate-DialectJava:DBunitils + Spring:不同的休眠方言
【发布时间】:2009-09-16 10:36:43
【问题描述】:

我目前使用 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?

谢谢!

【问题讨论】:

    标签: java spring dbunit


    【解决方案1】:

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

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

    【讨论】:

    • 感谢您的回答,但如果我从 ApplicationContext.xml 中删除方言规范,我会收到此错误:org.springframework.orm.hibernate3.HibernateSystemException:方言未设置。设置属性 hibernate.dialect.;嵌套的异常是 org.hibernate.HibernateException:未设置方言。设置属性 hibernate.dialect。
    【解决方案2】:

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

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

    【讨论】:

    • 这对我有什么帮助?我是否必须将整个会话工厂 bean 复制到第二个 ApplicationContext.xml?即使我将所有常量放入一个属性文件中,我也必须保持文件同步(例如 annotatedClasses)。
    猜你喜欢
    • 1970-01-01
    • 2020-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-03
    • 2018-10-23
    相关资源
    最近更新 更多