首页 ┆ 网站地图 ┆ 在线留言 ┆ 游戏资讯 ┆ 资源下载 ┆ 端午节祝福 ┆ 迅雷在线影视 ┆淘宝手机在线充值 ┆淘宝游戏点卡充值 
设为首页
加入收藏
联系我们
高级搜索
您当前的位置: 主页>JAVA专区>SPRING>java中利用spring动态的创建hibernate的配置文件
java中利用spring动态的创建hibernate的配置文件
来源: 发布时间:2008-06-10 发布人: 浏览: 人次   字体: [ ]  
想在java中动态的创建hibernate的配置文件(并不是生成而是创建hibernate配置文件对象),脑海中第一个想到的就是spring源码,因为spring有整合hibernate的功能,于是翻阅了spring的代码一看,果然很简单。下面先发断简单的demo。
 public static void main(String[] args) throws Exception {
        Configuration configuration 
= new Configuration();
        Properties properties 
= new Properties();
        properties.setProperty(
"hibernate.dialect",
                
"org.hibernate.dialect.SQLServerDialect");
        String[] mappingResources 
= new String[1];
        mappingResources[
0= "com/syj/domain/Issue.hbm.xml";
        
for (int i = 0; i < mappingResources.length; i++) {
            Resource resource 
= new ClassPathResource(mappingResources[i]
                    .trim(), Thread.currentThread().getContextClassLoader());
            configuration.addInputStream(resource.getInputStream());
        }
        configuration.addProperties(properties);
        configuration.buildSessionFactory();
    }

上面代码中properties还缺少很多属性,因为只是个demo所以我没有填写全部的属性,其实这里的属性就是指hibernate.cfg.xml中那些属性,例如hibernate.connection.provider_class,show_sql等等。添加到properties中就可以了。

为什么想到spring的源码呢,下面来看spring中如何整合的

<bean id="sessionFactory"
        class
="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        
<property name="dataSource">
            
<ref bean="dataSource" />
        
</property>
        
<property name="hibernateProperties">
            
<props>
                
<prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
                
<prop key="hibernate.show_sql">false</prop>
                
<prop key="hibernate.format_sql">false</prop>
                
<prop key="hibernate.use_sql_comments">false</prop>
                
<!-- 为单向关联(一对一, 多对一)的外连接抓取(outer join fetch)树设置最大深度. 值为0意味着将关闭默认的外连接抓取 -->
                
<prop key="hibernate.max_fetch_depth">3</prop>
                
<!-- 为Hibernate关联的批量抓取设置默认数量 -->
                
<prop key="hibernate.default_batch_fetch_size">8</prop>
                
<!-- 强制Hibernate按照被更新数据的主键,为SQL更新排序。这么做将减少在高并发系统中事务的死锁。 -->
                
<prop key="hibernate.order_updates">true</prop>
                
<!-- session在事务完成后将被自动清洗(flush) -->
                
<prop key="hibernate.transaction.flush_before_completion">true</prop>
                
<!-- Oracle限制那些通过JDBC驱动传输的字节数组的数目. 如果你希望使用二进值 (binary)或 可序列化的 (serializable)类型的大对象, 你应该开启 hibernate.jdbc.use_streams_for_binary属性.  -->
                
<prop key="hibernate.bytecode.use_reflection_optimizer">true</prop>
            
</props>
        
</property>
        
<property name="mappingResources">
            
<list>
                
<value>com/xxx/domain/xxx.hbm.xml</value>
             
</list>
       
</property>
</bean>

大家都知道spring的依赖注入,其实依赖注入很简单这里不做此介绍,我们按照spring的注入流程思考就可以了,下面看看spring优雅的实现把。在这里我们只关注LocalSessionFactoryBean的一个方法:protected SessionFactory buildSessionFactory() throws Exception {
        
// Create Configuration instance.
        Configuration config = newConfiguration();

        DataSource dataSource 
= getDataSource();
        
if (dataSource != null) {
            
// Make given DataSource available for SessionFactory configuration.
            configTimeDataSourceHolder.set(dataSource);
        }
        
if (this.jtaTransactionManager != null) {
            
// Make Spring-provided JTA TransactionManager available.
            configTimeTransactionManagerHolder.set(this.jtaTransactionManager);
        }
        
if (this.lobHandler != null) {
            
// Make given LobHandler available for SessionFactory configuration.
            
// Do early because because mapping resource might refer to custom types.
            configTimeLobHandlerHolder.set(this.lobHandler);
        }

        
// Analogous to Hibernate EntityManager's Ejb3Configuration:
        
// Hibernate doesn't allow setting the bean ClassLoader explicitly,
        
// so we need to expose it as thread context ClassLoader accordingly.
        Thread currentThread = Thread.currentThread();
        ClassLoader threadContextClassLoader 
= currentThread.getContextClassLoader();
        
boolean overrideClassLoader =
                (
this.beanClassLoader != null && !this.beanClassLoader.equals(threadContextClassLoader));
        
if (overrideClassLoader) {
            currentThread.setContextClassLoader(
this.beanClassLoader);
        }

        
try {
            
if (this.jtaTransactionManager != null) {
                
// Set Spring-provided JTA TransactionManager as Hibernate property.
                config.setProperty(
                        Environment.TRANSACTION_MANAGER_STRATEGY, LocalTransactionManagerLookup.
class.getName());
                config.setProperty(
                        Environment.TRANSACTION_STRATEGY, JTATransactionFactory.
class.getName());
            }
            
else {
                
// Set connection release mode "on_close" as default.
                
// This was the case for Hibernate 3.0; Hibernate 3.1 changed
                
// it to "auto" (i.e. "after_statement" or "after_transaction").
                
// However, for Spring's resource management (in particular for
                
// HibernateTransactionManager), "on_close" is the better default.
                config.setProperty(Environment.RELEASE_CONNECTIONS, ConnectionReleaseMode.ON_CLOSE.toString());
            }


共2页: 上一页 1 [2] 下一页
相 关 文 章   发布商链接
·Quartz的配置及使用和定时触发功能的...
·使用Spring集成XFire开发WebServic...
·Spring jar包详解
·为什么Spring成为SOA开发的首选Java...
·Spring框架简介及其Spring事务管理应...
·实例讲解spring整合struts的几种方式
 §最新评论:(评论内容只代表网友观点,与本站立场无关!)
网名: 验证码:  【所有评论】【↑返回顶部
评 分: 12 345
评论内容:(不能超过500字,请自觉遵守互联网相关政策法规。[按 Ctrl+Enter 可直接提交]
注意:请勿在本站发布政治话题、色情及违反法律的内容。
IT知道网 声明:刊登此文章是为了传递更多信息,文章内容仅供参考,转载请注明出处。
推 荐 文 章
·Quartz的配置及使用和定时触...
·实例讲解spring整合struts的...
·Spring框架简介及其Spring事...
·为什么Spring成为SOA开发的首
·Spring jar包详解
·使用Spring集成XFire开发WebS...
热 门 文 章
·实例讲解spring整合struts的...
·Spring框架简介及其Spring事...
·Spring jar包详解
·为什么Spring成为SOA开发的首...
·Quartz的配置及使用和定时触...
·使用Spring集成XFire开发WebS...
网站首页 - 关于本站 - 加入收藏 - 网站地图 - 友情连接 - 在线留言 - 联系我们 - 返回顶部
Copyright © 2007 IT知道网.[冀ICP备07026896号]. All Rights Reserved .