0

We configure our Session Factory bean in XML. The default session name is read from a properties file that is in the resource folder of the code. For a new purpose, I need to be able to change the default schema property to something else at runtime. I cannot figure out how.

The session factory is created in the XML wiring as follows:

   <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.default_schema">${oracle.default_schema}</prop>
            <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
            <prop key="hibernate.generate_statistics">false</prop>
            ... other properties ...
        </props>
    </property>
    ... lots of mapping files and annotated classes ...

I thought I could pull the session factory out of Spring's context, perhaps provide a Properties class to (I hoped) override the default session property and nothing else, and then continue on my way happily.

Unfortunately, when I try to get the session factory bean out of the context, it's of type SessionFactoryImpl and not AnnotationSessionFactoryBean. This means the setHibernateProperties() method is not available to me.

I'm not sure if I can call ALTER SESSION either. Because the session factory is obtained by our DAO classes in a library, I don't have an easy way of wrapping the session factory with the necessary code.

Any ideas?

Marvo
  • 17,845
  • 8
  • 50
  • 74

1 Answers1

0

Create multiple JPA config files, one for each environment you want to support, and then import it into your spring config.

<import resource="classpath:services-environment-${environment.name}.xml"/>

For me, there's a file for prod, test, and for each user, by setting the environment.name system property, the correct JPA configuration is loaded.

Scott Sosna
  • 1,443
  • 1
  • 8
  • 8