Java getCurrentSession调用在openSession之后立即失败

Java getCurrentSession调用在openSession之后立即失败,java,spring,hibernate,Java,Spring,Hibernate,您好,我已经使用hibernate通过事务管理器配置了sessionFactory和MysqL数据源。当我尝试在openSession()之后调用该工厂的getCurrentSession()时,它会抛出HibernateeException。我如何让它工作 错误跟踪: org.hibernate.HibernateException: No Session found for current thread at org.springframework.orm.hibernate4.Spring

您好,我已经使用hibernate通过事务管理器配置了sessionFactory和MysqL数据源。当我尝试在openSession()之后调用该工厂的getCurrentSession()时,它会抛出HibernateeException。我如何让它工作

错误跟踪:

org.hibernate.HibernateException: No Session found for current thread
at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:97)
at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:978)
at com.xyz.CommonTest.initTx(CommonTest.java:56)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:616)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
弹簧配置:

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="relationalDataSource"></property>
    <property name="packagesToScan">
        <list>
            <value>....</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">${hibernate.dialect}</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}  </prop>
            <prop key="hibernate.format_sql">true</prop>
            <prop key="hibernate.order_updates">true</prop>
        </props>
    </property>
</bean>

<tx:annotation-driven transaction-manager="transactionManager" />

<bean id="transactionManager"
    class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"></property>
</bean>
编辑:

您可以使用

休眠。当前会话上下文类=线程


如果你正在寻找一种自动的方法来实现这一点,那么Ryan下面的答案是正确的

仅仅打开会话不会将其与线程关联。无论如何,您不应该手动打开会话。会话应该由Spring(1)或(2)为您打开和关闭,作为某种视图中开放会话实现的一部分,Spring提供了。使用这两个选项中的任何一个,您都可以确保会话得到了正确的管理和清理,并且其中一个会话将与线程关联,因此您可以使用
getCurrentSession()

是的,我找到了它。我需要使用线程配置来自动执行此操作。在openSession调用中。我需要为单元测试手动调用它。我没有使用spring的注释实现单元测试。这是一个简单的junit实现,可能不是您想要的,除非您仅为测试设置它。我已经有一段时间没有看过代码的这一部分了,但我很确定这将对Spring对会话的管理造成严重破坏。如果您真的不想使用Spring的测试框架,这对于测试DAO来说是非常了不起的,那么您应该能够使用手动绑定会话来绑定和解除绑定会话。我已经将它设置为一个属性,对于测试,该属性是thread,而对于其余的则是none。这不管用吗?
    logger.info("initTx called");
    // Either get a current session or open new one.
    Session session = null;
    try {
        session = sessionFactory.getCurrentSession();
    } catch (HibernateException exception) {
        logger.info("did not find a current session, will try to open a new one",
                    exception);
    }
    session = sessionFactory.openSession();
    tx = session.beginTransaction();

    try {
        session = sessionFactory.getCurrentSession();
    } catch (HibernateException exception) {
        logger.info("did not find a current session, will try to open a new one",
                    exception);
    }