how to rebuild hibernate sessionFactory?

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.


<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?
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值