java中利用spring动态的创建hibernate的配置文件

 想在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());
            }

 

  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 打赏
    打赏
  • 扫一扫,分享海报

参与评论 您还未登录,请先 登录 后发表或查看评论
©️2022 CSDN 皮肤主题:大白 设计师:CSDN官方博客 返回首页

打赏作者

liangming6

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值