Java: DBunitils + Spring: different Hibernate-Dialect

I am currently using spring for dependency injection. Hibernate uses postgres dialogs for normal startup, but I want to use HSQL to test my DBUnitils database.

My Spring config contains the following:                                     

<!-- 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 are replaced by maven resource filtering.

Spring-Configruation for DBUnitils contains the following:

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

      

and this way overrides the dataSource from my launch config with UnitilsDataSource.

Problem: I am unable to run tests using Postgres-Dialect against the HSQL-Test database as some commands do not work. The only solution that came to my mind was to switch the resource filter in maven, but I have to do it manually (by testing "-P TEST" on every maven call). So there is no way to override hibernate.dialect?

Thank!

0


source to share


2 answers


You can see how to use PropertyPlaceHolderConfigurer in spring to change configuration. So you only need to provide different config file for different environments, spring xml will stay the same.



And you can load a config file eg.

0


source


Usually you don't need to specify the dialect at all, Hibernate will figure it out by looking at the underlying datasource. You only need to specify the dialect if you want to force Hibernate to use a specific one.



In your case, you should just remove the dialect property entirely and it should work in postgres and hsql without changing the config.

+4


source







All Articles