<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
  <channel>
    <title>auauau</title>
    <description></description>
    <link>http://auauau.javaeye.com</link>
    <language>UTF-8</language>
    <copyright>Copyright 2003-2008, JavaEye.com</copyright>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <generator>JavaEye - 做最棒的软件开发交流社区</generator>
      <item>
        <title>how to rebuild hibernate sessionFactory? </title>
        <author>auauau</author>
        <description>
          <![CDATA[
          <br/>
          作者: <a href="http://auauau.javaeye.com">auauau</a>&nbsp;
          链接：<a href="http://auauau.javaeye.com/blog/164582" style="color:red;">http://auauau.javaeye.com/blog/164582</a>&nbsp;
          发表时间: 2008年02月24日
          <br/><br/>
          声明：本文系JavaEye网站发布的原创博客文章，未经作者书面许可，严禁任何网站转载本文，否则必将追究法律责任！
          <br/><br/>
          <a href="http://forum.springframework.org/showthread.php?t=48214&highlight=LocalSessionFactoryBean" target="_blank">http://forum.springframework.org/showthread.php?t=48214&highlight=LocalSessionFactoryBean</a><br />I modify hibernate mapping file to add some columns for dynamic-component at runtime,I need it take effect at runtime.<br /><br />way 1,don't rebuild sessionFactory and use the original sessionFactory:<br />1.get entity's PersistentClass use ctx.getBean("&sessionFactory").getConfiguration(). getClassMapping(entityClassName)<br />and modify it<br />2.call ctx.getBean("&sessionFactory").updateDatabaseSchem a();<br /><br />but it doesn't works at all,it doesn't update database schema,and generated sql doesn't include those columns.<br />anything else I need to do with sessionFactory or configuration?<br /><br />way 2,rebuild sessionFactory using a sessionFactory proxy:<br />1.set hibernate properties use hibernate.hbm2ddl.auto=update<br />2.change original sessionFactory's id to '_sessionFactory',and add two beans.<br /><br />PHP Code:<br />&lt;bean id="sessionFactory" <br />        class="org.springframework.aop.framework.ProxyFactoryBean"> <br />        &lt;property name="targetSource" <br />            ref="refreshableSessionFactoryTargetSource" /> <br />&lt;/bean> <br />&lt;bean id="refreshableSessionFactoryTargetSource" class="RefreshableSessionFactoryTargetSource" /> <br /><br />&lt;bean id="_sessionFactory" <br />        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <br />        &lt;property name="mappingLocations"> <br />            &lt;list> <br />                &lt;value> <br />                    classpath*:resources/hibernate/**/*.hbm.xml <br />                &lt;/value> <br />            &lt;/list> <br />        &lt;/property> <br />&lt;/bean> <br /><br />PHP Code:<br />import java.lang.reflect.Method; <br /><br />import org.hibernate.SessionFactory; <br />import org.springframework.aop.target.dynamic.AbstractRefreshableTargetSource; <br />import org.springframework.beans.factory.InitializingBean; <br />import org.springframework.beans.factory.annotation.Autowired; <br />import org.springframework.context.ApplicationContext; <br />import org.springframework.orm.hibernate3.LocalSessionFactoryBean; <br /><br />public class RefreshableSessionFactoryTargetSource extends <br />        AbstractRefreshableTargetSource implements InitializingBean { <br /><br />    @Autowired <br />    private ApplicationContext ctx; <br /><br />    private LocalSessionFactoryBean lsfb; <br /><br />    private SessionFactory target; <br /><br />    private boolean needRefresh = false; <br /><br />    public void afterPropertiesSet() throws Exception { <br />        lsfb = (LocalSessionFactoryBean) ctx.getBean("&_sessionFactory"); <br />        target = (SessionFactory) lsfb.getObject(); <br />    } <br /><br />    protected synchronized Object freshTarget() { <br />        if (!needRefresh) { <br />            needRefresh = true; <br />            return target; <br />        } <br />        target.close(); <br />        target = null; <br />        try { <br />            Method method = LocalSessionFactoryBean.class <br />                    .getDeclaredMethod("buildSessionFactory"); <br />            method.setAccessible(true); <br />            target = (SessionFactory) method.invoke(lsfb); <br />            return target; <br />        } catch (Exception e) { <br />            throw new RuntimeException(e); <br />        } <br />    } <br /><br />    public SessionFactory getSessionFactory() { <br />        return target; <br />    } <br /><br />} <br /><br />3.call ctx.getBean("refreshableSessionFactoryTargetSource ").refresh();<br /><br />it works fine,but got a exception when execute a hibernate query<br /><br />Caused by: org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation <br /><br />of non-transactional one here <br />at org.springframework.orm.hibernate3.SpringSessionCo ntext.currentSession(SpringSessionContext.java:63) <br />at org.hibernate.impl.SessionFactoryImpl.getCurrentSe ssion(SessionFactoryImpl.java:544)<br />at sun.reflect.GeneratedMethodAccessor77.invoke(Unkno wn Source)<br />at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)<br />at java.lang.reflect.Method.invoke(Method.java:585)<br />at org.springframework.aop.support.AopUtils.invokeJoi npointUsingReflection(AopUtils.java:301) <br />at org.springframework.aop.framework.JdkDynamicAopPro xy.invoke(JdkDynamicAopProxy.java:198) <br />at $Proxy11.getCurrentSession(Unknown Source)<br /><br />I'am not using HibernateDaoSupport and use sessionFactory.getCurrentSession() directly in my Dao<br />it will execute SessionFactoryUtils.doGetSession(this.sessionFacto ry, false) in <br /><br />org.springframework.orm.hibernate3.SpringSessionCo ntext,but if I switch to original sessionFactory,it wouldn't execute <br /><br />SessionFactoryUtils.doGetSession(this.sessionFacto ry, false)<br />is this difference caused by sessionFactory is a proxy?how can I resolve this problem?
          <br/>
          <span style="color:red;">
            <a href="http://auauau.javaeye.com/blog/164582#comments" style="color:red;">本文的讨论也很精彩，浏览讨论>></a>
          </span>
          <br/><br/><br/>
          <span style="color:#E28822;">JavaEye推荐</span>
          <br/>
          <ul class='adverts'><li><a href='/adverts/42' target='_blank'><span style="color:red;font-weight:bold;">搜狐网站诚聘Java、PHP和C++工程师</span></a></li><li><a href='/adverts/41' target='_blank'><span style="color:red;font-weight:bold;">北京: 千橡集团暨校内网诚聘软件研发工程师</span></a></li></ul>
          <br/><br/><br/>
          ]]>
        </description>
        <pubDate>Sun, 24 Feb 2008 16:50:50 +0800</pubDate>
        <link>http://auauau.javaeye.com/blog/164582</link>
        <guid>http://auauau.javaeye.com/blog/164582</guid>
      </item>
  </channel>
</rss>