本帖最后由 smallfish25 于 2013-03-06 15:06:23 编辑

解决方案 »

  1.   

    感觉你的getSessionFactory()为空。看下数据源的配置文件
      

  2.   

        <session-factory>
            <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
            <property name="connection.url">jdbc:mysql://localhost:3306/myshare</property>
            <property name="connection.username">root</property>
            <property name="connection.password"></property>
            <property name="connection.pool_size">2</property>
            <property name="hbm2ddl.auto">update</property>
            <property name="hibernate.connection.autocommit">true</property>
            <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
            <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
            <property name="show_sql">true</property>       <!-- <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
            <property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
            <property name="connection.username">scott</property>
            <property name="connection.password">tiger</property>
            <property name="connection.pool_size">2</property>
            <property name="hibernate.connection.autocommit">true</property>
            <property name="dialect">org.hibernate.dialect.OracleDialect</property>
            <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
            <property name="show_sql">true</property>
            <property name="hbm2ddl.auto">create</property>-->        <mapping resource="com/share/entity/User.hbm.xml"/>
            
        </session-factory>
    这个getSessionFactory()是hibernateDaoSupport中的,在启动的时候已经把sessionfactory注入到了hibernateDaoSupport中,/*     */   public final SessionFactory getSessionFactory()
    /*     */   {
    /*  89 */     return ((this.hibernateTemplate != null) ? this.hibernateTemplate.getSessionFactory() : null);
    /*     */   }
      

  3.   

    打印getSessionFactory(),看是否有值
      

  4.   

    在高度的时候是能够得到sessionFactory的,在getDataSource方法中的实现方法是这样的。/*     */   public static DataSource getDataSource(SessionFactory sessionFactory)
    /*     */   {
    /* 123 */     if (sessionFactory instanceof SessionFactoryImplementor) {
    /* 124 */       ConnectionProvider cp = ((SessionFactoryImplementor)sessionFactory).getConnectionProvider();
    /* 125 */       if (cp instanceof LocalDataSourceConnectionProvider) {
    /* 126 */         return ((LocalDataSourceConnectionProvider)cp).getDataSource();
    /*     */       }
    /*     */     }
    /* 129 */     return null;
    /*     */   }
    通过上面的这个方法后得到的DataSource就为空了。
      

  5.   


    org.hibernate.impl.SessionFactoryImpl@aa5672
      

  6.   

    getSessionFactory()有值,而SessionFactoryUtils.getDataSource(getSessionFactory())为空?那真是奇怪了。
      

  7.   

    问题解决了,原来是jar包不兼容导致的。具体原因不清楚,但是换了jar包后可以使用了。