hibernate.cfg.xml
是Hibernate 默然加载的配置文件.
如果自己写的 配置文件.那么在创建连接工厂的时候就
必须手动加载.
Configuration myConfig = new Configuration().configure("ApplicationContext.xml");
SessinFactory sessinFactory = myConfig.buildSessionFactory();

解决方案 »

  1.   

    但是问题是我都是用的Spring的XMLBeanFactory的getBean方法取得实例的,照applicationcontext.xml的设置,Spring里应该已经被注入Hibernate.cfg.xml里的SessionFactory了啊
      

  2.   

    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="configLocation">
    <value>classpath:/com/elone/pm/hibernate.cfg.xml</value>//你HIBERNATE.CFG.XML的具体路径``有可能你HIBERNATE和APPLICATION的路径放的不是一个地方```不能取相对路径
    </property>
    </bean> 这样写看下
      

  3.   

        <bean id="sessionFactory"
            class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
            <property name="configLocation"
                value="file:src/hibernate.cfg.xml">//这里改下```
            </property>
        </bean>
      

  4.   

    你的事务怎么是挂在dao上呢,使用spring还挂dao上? 应该挂到业务层才好啊  .
      

  5.   

    没有用,我试过了,既然我执行的时候没有报错,同时连SQL语句都显示出来了,那么就说明Spring是找到了Hibernate.cfg.xml的
      

  6.   

    看错了呵呵, 我也发一段配置文件吧:<?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"><beans>
    <!-- dataSource -->
    <bean id="dataSource"
    class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName"
    value="com.microsoft.jdbc.sqlserver.SQLServerDriver">
    </property>
    <property name="url"
    value="jdbc:microsoft:sqlserver://192.168.0.9:1433;databaseName=dbxx;">
    </property>
    <property name="username" value="sa"></property>
    <property name="password" value="11111111"></property>
    </bean>
    <!--hibernate事务-->
    <bean id="transactionManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory">
    <ref local="sessionFactory" />
    </property>
    </bean>
    <!-- 基础事务代理 -->
    <bean id="baseTxProxy" abstract="true"
    class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
    <property name="transactionManager">
    <ref local="transactionManager" />
    </property>
    <property name="transactionAttributes">
    <props>
    <prop key="save*">PROPAGATION_REQUIRED,-Throwable</prop>
    <prop key="remove*">
    PROPAGATION_REQUIRED,-Throwable
    </prop>
    <prop key="merge">PROPAGATION_REQUIRED,-Throwable</prop>
    <prop key="update">PROPAGATION_REQUIRED,-Throwable</prop>
    <prop key="do*">PROPAGATION_REQUIRED,-Throwable</prop>
    </props>
    </property>
    </bean>
    <!-- sqlMapClient -->
    <bean id="sqlMapClient"
    class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
    <property name="configLocation">
    <value>WEB-INF/classes/sql-map-config-sqlserver.xml</value>
    </property>
    </bean>
    <!-- Hibernate的sessionFactory工厂 -->
    <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/cqry/domain/Client.hbm.xml</value>
    </list>
    </property>
    </bean>
    <!-- locator -->
    <bean name="cqry.locator"
    class="com.xxx.cqry.server.ServerLocator">
    <property name="clientServer">
    <ref local="clientServer" />
    </property>
    </bean>
    <!-- daoFacade -->
    <bean id="daoFacade" class="com.xxx.cqry.dao.DaoFacade">
    <property name="clientDao">
    <ref local="clientDao" />
    </property>
    </bean>
    <bean id="baseDao" class="com.xxx.cqry.dao.BaseDAO">
    <property name="sessionFactory">
    <ref local="sessionFactory" />
    </property>
    <property name="dataSource">
    <ref bean="dataSource" />
    </property>
    <property name="sqlMapClient">
    <ref local="sqlMapClient" />
    </property>
    </bean>
    <bean id="clientDao" class="com.xxx.cqry.dao.ClientDAO" parent="baseDao"></bean>
    <!-- baseServer -->
    <bean id="baseServer"
    class="com.xxx.cqry.server.impl.BaseServerImpl">
    <property name="daoFacade">
    <ref local="daoFacade" />
    </property>
    </bean>
    <bean id="clientServer" parent="baseTxProxy">
    <property name="target">
    <bean class="com.xxx.cqry.server.impl.ClientServerImpl"
    parent="baseServer" />
    </property>
    </bean>
    </beans>