java - Spring Hibernate: reload entity mappings -
on web application use spring 3.2 , hibernate 4.1.1 , implement plugin-like architecture. plugins can added , removed on runtime. each module defined separate class loader , create separate child application context on spring. complete configuration done using annotations, no xml config beans anymore.
spring hibernate configuration class
@configuration @enabletransactionmanagement public class hibernateconfigurationfactory { @bean public jndiobjectfactorybean datasource() { jndiobjectfactorybean ds = new jndiobjectfactorybean(); ds.setjndiname("java:jboss/datasources/ourownds"); ds.setresourceref(true); return ds; } @bean public localsessionfactorybean sessionfactory() { localsessionfactorybean sessionfactory = new localsessionfactorybean(); sessionfactory.setpackagestoscan("com.foo.bar"); sessionfactory.setdatasource((datasource) datasource().getobject()); properties hibernateproperties = new properties(); hibernateproperties.put("hibernate.hbm2ddl.auto", "update"); sessionfactory.sethibernateproperties(hibernateproperties); return sessionfactory; } @bean public hibernatetransactionmanager transactionmanager() { hibernatetransactionmanager transactionmanager = new hibernatetransactionmanager(); transactionmanager.setsessionfactory(sessionfactory().getobject()); return transactionmanager; } } now problem: plugins contain own entity (+dao) classes added module during runtime.
is possible create kind of separate context on hibernate (as on spring) or add/reload additional entity classes?
will reloading entitymanager fit needs? happen loaded entities in context?
thanks , comment in advance.
update: did following , solved issue (but ran other issue.. later on).
i create new datasource + sessionfactory + transactionmanager each module/context , insert them new child applicationcontext. use class loader scann annotated classes , register them manually on application context , session factory using
localsessionfactorybean#setannotatedclasses(...) this works quite well... but...
next problem: classnotfoundexception seems class loader issue. hibernate uses system class loader instead of own pluginclassloader.
does know how inject own class loader hibernate?
is feasible inject environment , use source hibernate resources?
@configuration @enabletransactionmanagement public class hibernateconfigurationfactory { @autowired environment env; @bean public localsessionfactorybean sessionfactory() { localsessionfactorybean sessionfactory = new localsessionfactorybean(); sessionfactory.setpackagestoscan("com.foo.bar"); if (env.containsproperty("some.extra.classes.property") { sessionfactory.setannotatedclasses(some extrapolation here); // or add packages scanning ... } } } as dao beans, can use @profile that, or - if using spring 4 - use @conditional.
edit
the environment not create, it's "there you" . it's container property source , profile sets.
a reference point spring reference documentation, ioc container chapter. can @ springsource blog. there few article chris beams, related spring 3.1, of stuff there.
as example, can use bellow bootstrap child application context:
annotationconfigapplicationcontext context = new annotationconfigapplicationcontext(); configurableenvironment environment = context.getenvironment(); // how set different properties per sub context. map subcontextmap = new hashmap(); subcontextmap.put("some.extra.classes.property", [unique value here]); environment.getpropertysources().addfirst(new mappropertysource("sub_ctx_map", subcontextmap); // generic configuration class(es). context.register(hibernateconfigurationfactory.class); context.refresh();
Comments
Post a Comment