Hopefully this post makes sense and someone can assist me. I have also posted this in the Spring User's forum because even though it is a Hibernate question, I'm attempting to use Spring's support functionality.
My organization partitions its data into multiple schema's. In my example we would have a single table, defined as follows:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="my.package.domain.model">
<class name="Person" table="PERSON" lazy="false" >
<id name="id" column="ID" type="java.lang.String" >
<generator class="assigned" />
</id>
<property name="givenName" column="GIVEN_NAME" type="java.lang.String" not-null="true" />
<property name="surname" column="SURNAME" type="java.lang.String" not-null="true" />
<property name="birthDate" column="BIRTH_DATE" type="java.util.Date" not-null="true" />
</class>
</hibernate-mapping>
This table would actually exist in 5 schemas. Depending on some property of the object, for this example we'll use birth dates, the object will be persisted to a specific schema.
ie:
if the person's brithday is before 1950, it goes in "SCHEMA_A"
if the person's birthday is after 1950 but before 1970, it goes in "SCHEMA_B",
etc.
Now, since we're using Spring & Hibernate, we want to have the following spring bean definitions:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="DataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="jdbc/MyDataSource" />
</bean>
<bean id="MySessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="mappingResources">
<list>
<value>Person.hbm.xml</value>
<value>PersonSchemaMap.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.DB2Dialect</prop>
<prop key="hibernate.default_schema">DEFAULT</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.format_sql">false</prop>
<prop key="hibernate.query.substitutions">true 'Y', false 'N', yes 'Y', no 'N'</prop>
</props>
</property>
<property name="dataSource">
<ref bean="MyDataSource" />
</property>
</bean>
<bean id="PersonDao" class="my.package.dao.hibernate.HibernatePersonDao" >
<!-- Extends Spring's HibernateDaoSupport class -->
<property name="sessionFactory" ref="MySessionFactory" />
</bean>
</beans>
The Crux of the problem:
All of the Schemas exist in the same Database, so the same SessionFactory should suffice. Except that when defined, the SessionFactory's schema is set and cannot be changed.
Some gotchas: The DAO's all extend the HibernateDaoSupport class of Spring, which allows us to properly manage transactions and isolation levels, in a declarative manner. Therefore we can't go arbitrarily creating new SessionFactories (via programmatic interface), or we'll break the Spring support for this.
We can't use multiple SessionFactory instances, as this would require we define multiple DAO bean instances, which in turn would require that anything using it know which bean-id to look for (Kinda defeats the purpose, no?)
What I'm really looking for is some way to...
intercept the session after the call to a "getHibernateTemplate()" method (eg: save) is made,
change the setting of the schema on it,
allow the original requested functionality to perform,
return the schema value to the previous setting,
return to the DAO that made the call to "getHibernateTemplate()" method.
OR - Change the stored Schema value on the Session before it builds the SQL, and change it back after
But all of that without breaking the transaction/isolation support and keeping it platform independent. I know that Hibernate has a project in the works for supporting something like this (Hibernate Shards) but I can't use a beta codebase for anything I'm doing.
Does this make sense? Is it possible with the current Spring/Hibernate frameworks, or am I out of luck til Hibernate Shards is complete?
Thanks for any help...
-B