I was able to subclass Hibernate's LocalSessionFactoryBean class, overriding the postProcessMappings method to dynamically pull in additional Hibernate-mapping xml definition files from the classpath. I then modified my Spring configuration to use it when initializing Hibernate's SessionFactory bean. This still must happen
before the Hibernate SessionFactory is instantiated.
For reference, here's a simplified version:
Code:
public class DynamicLocalSessionFactoryBean extends LocalSessionFactoryBean {
protected void postProcessMappings(Configuration config) throws HibernateException {
String[] resourceNames = findDynamicResources(); // Get array of resources on the classpath
ClassLoader classLoader= Thread.currentThread().getContextClassLoader();
for (String resourceName: resourceNames) {
Resource resource = new ClassPathResource(resourceName, classLoader);
config.addInputStream(resource.getInputStream());
}
}...
P.S. Hibernate's insistence on making SessionFactory immutable is perplexing. As long as a user understands there will be a (temporary) performance penalty related to reinitializing the environment, this should be allowed whenever desired.