-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Dynamic hibernate-config files
PostPosted: Thu Sep 20, 2012 6:29 pm 
Newbie

Joined: Thu Sep 20, 2012 6:02 pm
Posts: 2
I am looking for a method to dynamically add to the existing hibernate-mapping XML configuration files BEFORE Hibernate initializes, so they can be used by the application.

Besides our normal mapping files, we have customer submitted jars that we would like to just "drop in" to the war (before the application starts). There are many of these jars and they (and their associated files) change frequently, so we don't want to have to update the configuration files each time a change is made.

Some had suggested using the criteria API ( http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/querycriteria.html ). However, our queries are already in hibernate-mapping XML format, which is easier to manage and review. If it was possible to dynamically generate the queries from the hibernate-mapping XML, this *might* be an option.

P.S. I was hoping to build a hot-deploy environment where a "dropped in" module would dynamically update the existing Hibernate SessionFactory with queries/etc. after it had initialized. However, that is apparently not possible since SessionFactory is effectively immutable.

Currently in use: Spring 2.5.x and Hibernate 3.2.x (although we may be open to new versions if necessary)


Top
 Profile  
 
 Post subject: Re: Dynamic hibernate-config files
PostPosted: Wed Sep 26, 2012 4:14 pm 
Newbie

Joined: Thu Sep 20, 2012 6:02 pm
Posts: 2
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.