`
auauau
  • 浏览: 170792 次
社区版块
存档分类
最新评论

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?
分享到:
评论

相关推荐

    J2EE利用Hibernate采用B/S架构网页设计

    * Rebuild hibernate session factory * */ public static void rebuildSessionFactory() { try { configuration.configure(configFile); sessionFactory = configuration.buildSessionFactory(); } catch ...

    通过弗里曼链码算法提取图像边界

    里面有代码和数据集I = imread('coins.png');...% Example II : how to rebuild the boundary image from the boundary coordinates vectorbound_img2 = zeros(size(I));for k = 1:numel(bound_coord)

    DELL_服务器硬盘掉线后的REBUILD修复操作全过程

    ### DELL服务器硬盘掉线后的REBUILD修复操作详解 在企业级服务器运维中,RAID技术作为数据冗余和性能提升的重要手段,被广泛应用。然而,在长时间运行或遇到突发状况时,服务器硬盘可能会出现故障,导致RAID阵列...

    SQL置疑REPAIR_REBUILD修复

    SQL Server 修复工具 REPAIR_REBUILD SQL Server 是一个复杂的关系数据库管理系统,它提供了强大的数据存储和管理功能。但是,当 SQL Server 遇到 I/O 错误、磁盘问题或其他错误时,数据库可能会损坏,导致数据丢失...

    DelphiSpeedUpV16

    Do you want to rebuild?" dialog that appears if you have accidentally changed a file while debugging the application. === How to install === Simply start the InstallDelphiSpeedUpX.exe where X is ...

    sbt in Action(Manning,2015)

    and with sbt’s unique incremental compilation feature, you can update only the parts of your project that change, without having to rebuild everything. Mastering sbt, along with the right patterns ...

    Solar Board Rebuild on TI part4

    Solar Board Rebuild on TI part4

    W589_Rebuild_Tool.exe

    W589_Rebuild_Tool.exe

    Unity UI 重建(Rebuild)分析

    通过编写 MonoBehaviour 收集Unity UI 中的当前帧有那些元素触发了Rebuild。方便进行UI重建方面的性能检查,以便提升性能。 原理就是通过对 CanvasUpdateRegistry 中的属性值型判断收集实现。由于UGUI中...

    REBUILD 通过创新的业务流程引擎帮助你快速搭建各类企业管理系统,全图形化配置无需了解技术

    高度可配置化的企业管理系统!可免费商用!...REBUILD 侧重于业务需求实现,而非基础技术框架或项目启动模板,通过 REBUILD 可以真正实现零代码快速搭建!无需编程、无需编译代码,甚至无需了解任何技术。

    Fmod Bank Tools_GameEngine_TheFirst_

    How to use:First place bank files into the bank folder you want to extract. Once extracted you will find the wav files in the wav folder and another folder with the same name as the bank file.To ...

    rebuild_db-1.0-rc1.zip

    标题“rebuild_db-1.0-rc1.zip”表明这是一个软件或工具的打包文件,版本为1.0的候选发布版1(RC1)。在IT行业中,RC1通常代表Release Candidate 1,这意味着该软件已经接近最终版本,但在正式发布前还需要进行最后...

    generate_descriptor_proto.rar_rebuild

    This script is used to rebuild the Linux 32-bit cross-toolchain that allows you to generate 32-bit binaries that target Ubuntu 8.04.

    index rebuild

    根据提供的信息,我们可以总结出以下关于“索引重建(Index Rebuild)”的相关知识点: ### 索引重建概述 **索引重建**是数据库管理中的一个重要操作,它涉及删除现有的索引并重新创建它。这个过程可以帮助优化...

    Terrain To Mesh 2021 v2021.5

    Terrain To Mesh 2021 is new rebuild, upgraded and improved version of Terrain To Mesh which is on the market since 2015. Owners of the old (now deprecated) Terrain To Mesh can use price reduced ...

    WZ-Explorer-Rebuild-master_Explorer_tool_

    《WZ-Explorer-Rebuild-master:解构MapleStory文件与地图解压缩工具》 MapleStory,一款深受全球玩家喜爱的2D横版卷轴式网络游戏,以其独特的角色扮演和丰富的冒险故事吸引了大量忠实玩家。为了更好地理解和修改...

    数据库优化以及操作说明

    The SQL Server Maintenance Solution lets you intelligently rebuild or reorganize only the indexes that are fragmented. In the IndexOptimize procedure, you can define a preferred index maintenance ...

    Rebuild:Android版Rebuild.fm

    Android版Rebuild.fm Android的Rebuild.fm是收听Rebuild.fm的最佳播客播放器。 (非官方) 关于Rebuild.fm 的。 谈论技术,软件开发和小工具。 截屏 当前版本(0.10.0) 安装 建立在本地 $ git clone ...

    Android studio的make Project、clean Project、Rebuild Project

    Clean Project :删除build目录下的所有文件,并对项目的大部分文件进行重新编译,时间相对于Rebuild Project短点; Rebuild Project :包含了Clean的操作,并对整个项目进行了重新编译(包括NDK和资源文件),因此...

Global site tag (gtag.js) - Google Analytics