Configuring Hibernate properties in Spring 2
Okay, so here’s the deal.
I want to have Hibernate work against Oracle and HSQL. I want it to be configured through Spring, and I want it to not be the schema owner in Oracle.
Setting a different schema owner is easy in Hibernate. You specify this in a spring.properties file, using PropertyPlaceholderConfigurer:
hibernate.default_schema=db
and then you have a properly parameterized Hibernate through Spring:
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"><property name="configLocation"> <value>classpath:hibernate.cfg.xml</value> </property> <property name="dataSource"> <ref bean="dataSource" /> </property> <!-- Overrides what is defined in hibernate.cfg.xml --> <property name="hibernateProperties"> <props> <!-- properties chopped for space --> <prop key="hibernate.default_schema">${hibernate.default_schema}</prop> </props> </property></bean>
This works fine with Oracle. When you try a default schema with HSQL, it complains, because it doesn’t like the DB.table syntax.
However, setting hibernate.default_schema to an empty string doesn’t work. Even if you somehow hacked PropertyPlaceholderConfigurer to have ${null} as a value (which it won’t do, you can only do it through XML), it wouldn’t work, because a Properties object can’t have a null value.
The way to do this is to use a PropertiesFactoryBean, and define any “conditional” properties through the locations. Then you can define a property as hibernate.config.file=hibernate-oracle and have it pick up hibernate-oracle.properties with the hibernate.default_schema=${hibernate.default_schema} defined in there.
<bean id="hibernateProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean"><property name="properties"> <props> <!-- normal properties --> </props> </property> <!-- hibernate.config should be defined somewhere in the spring.properties layers --> <property name="locations"> <list> <value>classpath:${hibernate.config.file}.properties</value> </list> </property></bean>
<bean id=”sessionFactory” class=”org.springframework.orm.hibernate3.LocalSessionFactoryBean”>
<!-- normal stuff --> <property name="hibernateProperties"> <ref local="hibernateProperties" /> </property></bean>