【问题标题】:Spring Config for Hibernate Session休眠会话的 Spring 配置
【发布时间】:2011-05-03 06:25:15
【问题描述】:

嗨 我正在尝试通过Spring的注入来获取hibernate的Session。

这是我的 spring 上下文 xml:

  <!-- hibernate's session factory -->
  <bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="configLocation">
      <value>classpath:./hibernate.cfg.xml</value>
    </property>
  </bean>

  <!-- the transaction manager -->
  <bean id="transactionManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
  </bean>

代码如下:

private static ApplicationContext ctx;

    if (ctx == null) {
        ctx = new ClassPathXmlApplicationContext("springContext.xml");
    }
    LocalSessionFactoryBean sf = ctx.getBean(LocalSessionFactoryBean.class);
    session = sf.getObject().getCurrentSession();

但是我获得的会话是空的。

通过sf.getObject().getCurrentSession()获取Session是否正确?

谢谢:)

【问题讨论】:

    标签: hibernate spring configuration sessionfactory hibernate-session


    【解决方案1】:

    我认为您不应该这样访问会话。要么使用HibernateTemplate,要么在你的bean中注入SessionFactory,然后调用getCurrentSession()。否则您的事务管理将无法正确处理

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-16
      • 2014-11-05
      • 2021-12-17
      • 2013-07-26
      • 2021-09-28
      • 1970-01-01
      相关资源
      最近更新 更多