views:

361

answers:

2

I currently use spring for depency injection. Hibernate uses a postgres dialect for the normal run, but I want to use HSQL for the DBUnitils Databank-Testing.

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.

The Spring-Configruation for DBUnitils contains this:

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

and so overrides the dataSource from my run configuration with the UnitilsDataSource.

The Problem: I cant run the Tests using Postgres-Dialect against the HSQL-Test-Database because some commands dont work. 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). So isn't there a possibilty to override the hibernate.dialect?

Thanks!

+1  A: 

You normally 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 be able to remove the dialect property completely, and it should work in postgres and hsql without config modification.

skaffman
Thanks for the answer but if I remove the dialect-spec from the ApplicationContext.xml, I get this error:org.springframework.orm.hibernate3.HibernateSystemException: The dialect was not set. Set the property hibernate.dialect.; nested exc eption is org.hibernate.HibernateException: The dialect was not set. Set the property hibernate.dialect.
theomega
A: 

You should possibly look at using the PropertyPlaceHolderConfigurer in spring to change the config. That way you need only supply a different config file for the different environments, the spring xml stays the same.

And you can load the config file like this.

Michael Wiles
How does this help me? Do I have to copy to whole session-factory bean to a second ApplicationContext.xml? Even if I put all constants into a property-file, I would have to keep the files in sync (for example the annotatedClasses).
theomega