如何正确使用SessionFactory.getCurrentSession()?

时间:2012-09-09 17:21:14

标签: hibernate session sessionfactory

@Test
public void testGetCurrentSession(){

    Session firstSession=HibernateUtil.getSessionFactory().getCurrentSession();

    Session secondSession=HibernateUtil.getSessionFactory().getCurrentSession();

    assertEquals(firstSession,secondSession);
}

为什么此单元测试失败?它在同一个线程中,为什么getCurrentSession()会返回两个不同的会话?

我已在hibernate.cfg.xml中将' current_session_context_class '设置为'主题',如下所示

<property name="current_session_context_class">thread
</property>

ps:hibernate版本是4.0.1。

任何帮助将不胜感激!

0 个答案:

没有答案