2009-09-16 58 views
0

我目前使用spring来进行注入。 Hibernate使用postgres方言进行正常运行,但我想使用HSQL进行DBUnitils数据库测试。Java:DBunitils + Spring:不同的Hibernate-Dialect

Spring的配置包含此:

<!-- 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的资源过滤取代。

弹簧Configruation为DBUnitils包含此:

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

,因此从与UnitilsDataSource我跑的配置将覆盖数据源。

问题:我不能使用Postgres-Dialect对HSQL测试数据库运行测试,因为有些命令不工作。 我想到的唯一解决方案是:切换maven中的资源过滤器,但我必须手工完成(通过在每个maven调用中提供“-P TEST”)。那么重写hibernate.dialect是不是有可能?

谢谢!

回答

0

你应该看看在春天使用PropertyPlaceHolderConfigurer来改变配置。这样你只需要为不同的环境提供不同的配置文件,spring xml保持不变。

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

+0

这对我有帮助吗?我是否必须将整个会话工厂bean复制到第二个ApplicationContext.xml?即使我将所有常量放入属性文件中,我也必须保持文件同步(例如annotatedClasses)。 – theomega 2009-09-16 20:10:16

4

您通常不需要指定方言,Hibernate会通过查看底层数据源来计算出来。如果你想迫使Hibernate使用特定的方言,你只需要指定方言。

在你的情况,你应该只能够完全删除方言属性,它应该在postgres和hsql中工作,而不需要修改配置。

+1

感谢您的答案,但如果我从ApplicationContext.xml中删除dialect-spec,我得到这个错误: org.springframework.orm.hibernate3.HibernateSystemException:未设置方言。设置属性hibernate.dialect .;嵌套的异常是org.hibernate.HibernateException:未设置方言。设置属性hibernate.dialect。 – theomega 2009-09-16 19:57:09

相关问题