http://forum.springframework.org/showthread.php?t=48214&highlight=LocalSessionFactoryBean
I modify hibernate mapping file to add some columns for dynamic-component at runtime,I need it take effect at runtime.
way 1,don't rebuild sessionFactory and use the original sessionFactory:
1.get entity's PersistentClass use ctx.getBean("&sessionFactory").getConfiguration(). getClassMapping(entityClassName)
and modify it
2.call ctx.getBean("&sessionFactory").updateDatabaseSchem a();
but it doesn't works at all,it doesn't update database schema,and generated sql doesn't include those columns.
anything else I need to do with sessionFactory or configuration?
way 2,rebuild sessionFactory using a sessionFactory proxy:
1.set hibernate properties use hibernate.hbm2ddl.auto=update
2.change original sessionFactory's id to '_sessionFactory',and add two beans.
3.call ctx.getBean("refreshableSessionFactoryTargetSource ").refresh();
it works fine,but got a exception when execute a hibernate query
Caused by: org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation
of non-transactional one here
at org.springframework.orm.hibernate3.SpringSessionCo ntext.currentSession(SpringSessionContext.java:63)
at org.hibernate.impl.SessionFactoryImpl.getCurrentSe ssion(SessionFactoryImpl.java:544)
at sun.reflect.GeneratedMethodAccessor77.invoke(Unkno wn Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.springframework.aop.support.AopUtils.invokeJoi npointUsingReflection(AopUtils.java:301)
at org.springframework.aop.framework.JdkDynamicAopPro xy.invoke(JdkDynamicAopProxy.java:198)
at $Proxy11.getCurrentSession(Unknown Source)
I'am not using HibernateDaoSupport and use sessionFactory.getCurrentSession() directly in my Dao
it will execute SessionFactoryUtils.doGetSession(this.sessionFacto ry, false) in
org.springframework.orm.hibernate3.SpringSessionCo ntext,but if I switch to original sessionFactory,it wouldn't execute
SessionFactoryUtils.doGetSession(this.sessionFacto ry, false)
is this difference caused by sessionFactory is a proxy?how can I resolve this problem?
I modify hibernate mapping file to add some columns for dynamic-component at runtime,I need it take effect at runtime.
way 1,don't rebuild sessionFactory and use the original sessionFactory:
1.get entity's PersistentClass use ctx.getBean("&sessionFactory").getConfiguration(). getClassMapping(entityClassName)
and modify it
2.call ctx.getBean("&sessionFactory").updateDatabaseSchem a();
but it doesn't works at all,it doesn't update database schema,and generated sql doesn't include those columns.
anything else I need to do with sessionFactory or configuration?
way 2,rebuild sessionFactory using a sessionFactory proxy:
1.set hibernate properties use hibernate.hbm2ddl.auto=update
2.change original sessionFactory's id to '_sessionFactory',and add two beans.
<bean id="sessionFactory"
class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="targetSource"
ref="refreshableSessionFactoryTargetSource" />
</bean>
<bean id="refreshableSessionFactoryTargetSource" class="RefreshableSessionFactoryTargetSource" />
<bean id="_sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="mappingLocations">
<list>
<value>
classpath*:resources/hibernate/**/*.hbm.xml
</value>
</list>
</property>
</bean>
import java.lang.reflect.Method;
import org.hibernate.SessionFactory;
import org.springframework.aop.target.dynamic.AbstractRefreshableTargetSource;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.LocalSessionFactoryBean;
public class RefreshableSessionFactoryTargetSource extends
AbstractRefreshableTargetSource implements InitializingBean {
@Autowired
private ApplicationContext ctx;
private LocalSessionFactoryBean lsfb;
private SessionFactory target;
private boolean needRefresh = false;
public void afterPropertiesSet() throws Exception {
lsfb = (LocalSessionFactoryBean) ctx.getBean("&_sessionFactory");
target = (SessionFactory) lsfb.getObject();
}
protected synchronized Object freshTarget() {
if (!needRefresh) {
needRefresh = true;
return target;
}
target.close();
target = null;
try {
Method method = LocalSessionFactoryBean.class
.getDeclaredMethod("buildSessionFactory");
method.setAccessible(true);
target = (SessionFactory) method.invoke(lsfb);
return target;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public SessionFactory getSessionFactory() {
return target;
}
}
3.call ctx.getBean("refreshableSessionFactoryTargetSource ").refresh();
it works fine,but got a exception when execute a hibernate query
Caused by: org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation
of non-transactional one here
at org.springframework.orm.hibernate3.SpringSessionCo ntext.currentSession(SpringSessionContext.java:63)
at org.hibernate.impl.SessionFactoryImpl.getCurrentSe ssion(SessionFactoryImpl.java:544)
at sun.reflect.GeneratedMethodAccessor77.invoke(Unkno wn Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.springframework.aop.support.AopUtils.invokeJoi npointUsingReflection(AopUtils.java:301)
at org.springframework.aop.framework.JdkDynamicAopPro xy.invoke(JdkDynamicAopProxy.java:198)
at $Proxy11.getCurrentSession(Unknown Source)
I'am not using HibernateDaoSupport and use sessionFactory.getCurrentSession() directly in my Dao
it will execute SessionFactoryUtils.doGetSession(this.sessionFacto ry, false) in
org.springframework.orm.hibernate3.SpringSessionCo ntext,but if I switch to original sessionFactory,it wouldn't execute
SessionFactoryUtils.doGetSession(this.sessionFacto ry, false)
is this difference caused by sessionFactory is a proxy?how can I resolve this problem?