请问各位大虾们, 在Hibernate中 ,ApplicationContext ap = new ClassPathXmlApplicationContext("applicationContext.xml");
SessionFactory sessionFactory = (SessionFactory)ap.getBean("sessionFactory");
Session session1 = sessionFactory.openSession();
  与
  Session session2 = 
  getHibernateTemplate().getSessionFactory().openSession();
  session1与session2有何本质区别?
  为什么我在数据库中写一个存储过程,通过session1就可执行,而session2却报一个 '找不到存储过程'的错误

解决方案 »

  1.   

    如果确保你的HibernateTemplate注入的是同一个SessionFactory,结果应该相同吧。。
    再贴点配置或者代码,日志之类的看楼下的能不能帮到你 /**
     * Set the Hibernate SessionFactory that should be used to create
     * Hibernate Sessions.
     */
    public void setSessionFactory(SessionFactory sessionFactory) {
    this.sessionFactory = sessionFactory;
    } /**
     * Return the Hibernate SessionFactory that should be used to create
     * Hibernate Sessions.
     */
    public SessionFactory getSessionFactory() {
    return this.sessionFactory;
    }
      

  2.   

    第二个得不到东西是因为:ApplicationContext ap = new ClassPathXmlApplicationContext("applicationContext.xml");这句,如果是启动WEB应用,就正常了.
      

  3.   

    谢谢各位大虾的回答,但是我是通过Session session2 = 
    getHibernateTemplate().getSessionFactory().openSession();获取session报了个 ‘存储过程’的错误,然后才用ApplicationContext ap = new ClassPathXmlApplicationContext("applicationContext.xml");这种方式的。在我看来似乎两种方式获取的session对象没啥区别啊,为什么通过getHibernateTemplate就不行呢?
      

  4.   

    lz该类是继承了HibernateDaoSupport吧
    必须在spring配置文件中为它注入SessionFactory
    不然getHibernateTemplate()得到的HibernateTemplate里面的session是空的,
    当然就抛出异常啦
    public class PlaceManagerImpl extends HibernateDaoSupport implements PlaceManager
    <bean id="placeManager" class="czw.business.manager.PlaceManagerImpl">
        <property name="sessionFactory" ref="sessionFactory"/>
     </bean>
      

  5.   

    要在applicationContext中注入sessionFactory吧
    只要你启动web容器,session不应是空,再看看你的配置文件是否正确
      

  6.   

    我是继承HibernateDaoSupport的啊,并且也在applicationContext中注入了sessionFactory,通过getHibernateTemplate()获取的session不为空,执行HQL语句页正常,但是调用存储过程就报了个"找不到存储过程"的错误.
    而通过ApplicationContext ap = new ClassPathXmlApplicationContext("applicationContext.xml");获取的session调用存储过程则正常