Hibernate 未配置CurrentSessionContext!:ThreadLocalSessionContext.bind(会话);isn';我不能正确绑定我的会话

Hibernate 未配置CurrentSessionContext!:ThreadLocalSessionContext.bind(会话);isn';我不能正确绑定我的会话,hibernate,session,junit,Hibernate,Session,Junit,我正在使用Hibernate4.0.1.Final 测试服务时绑定会话时遇到问题。我使用ThreadLocalSessionContext.bind(session)尝试绑定会话,以便sessionFactory.getCurrentSession()返回一个有效的会话,但它不工作 调用sessionFactory.getCurrentSession()会产生异常(如下所示)。在我的服务课上,我有: public class OrganizationServiceImpl extends Abs

我正在使用Hibernate4.0.1.Final

测试服务时绑定会话时遇到问题。我使用
ThreadLocalSessionContext.bind(session)
尝试绑定会话,以便
sessionFactory.getCurrentSession()
返回一个有效的会话,但它不工作

调用
sessionFactory.getCurrentSession()
会产生异常(如下所示)。在我的服务课上,我有:

public class OrganizationServiceImpl extends AbstractTransactionService implements OrganizationService {

public void saveOrganization(final String eodbId,
                             final String parentEodbId,
                             final String name,
                             final String description,
                             final String organizationType,
                             final String url,
                             final String stateAbbrev) {
    Session session = null;
    try {
        session = startOperation();
        final State state = orgDao.findStateByAbbrev(stateAbbrev);

…

protected Session startOperation() throws HibernateException {
    final Session session = sessionFactory.openSession();
    ThreadLocalSessionContext.bind(session);
    tx = session.beginTransaction();
    return session;
}
下面是调用此服务的JUnit测试

@Before
public void setupOrgServiceTest() { 
    Configuration config = new Configuration()
        .setProperty("hibernate.dialect", "org.hibernate.dialect.HSQLDialect")
        .setProperty("hibernate.connection.driver_class", "org.hsqldb.jdbcDriver")
        .setProperty("hibernate.connection.url", "jdbc:hsqldb:mem:lyceaTestDb")
        .setProperty("hibernate.connection.username", "sa")
        .setProperty("hibernate.connection.password", "")
        .setProperty("hibernate.connection.pool_size", "1")
        .setProperty("hibernate.connection.autocommit", "true")
        .setProperty("hibernate.cache.provider_class", "org.hibernate.cache.HashtableCacheProvider")
        .setProperty("hibernate.hbm2ddl.auto", "create-drop")
        .setProperty("hibernate.show_sql", "true")
        .addAnnotatedClass(Organization.class)
        .addAnnotatedClass(State.class); 
    final ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
    sessionFactory = config.buildSessionFactory(serviceRegistry);
    orgService = new OrganizationServiceImpl(sessionFactory);
}   // setupOrgSErviceTest

@Test
public void testSaveOrg() { 
    orgService.saveOrganization(testProps.getProperty("test.school.eodbId"),
                                testProps.getProperty("test.school.parent.eodbId"),
                                testProps.getProperty("test.school.name"), 
                                testProps.getProperty("test.school.description"), 
                                testProps.getProperty("test.school.org.type"), 
                                testProps.getProperty("test.school.url"), 
                                testProps.getProperty("test.school.state"));
}   // testSaveOrg
调用
sessionFactory.getCurrentSession()
时遇到的异常是

org.myco.myproject.orgsclient.exceptions.DataAccessLayerException: org.hibernate.HibernateException: No CurrentSessionContext configured!
    at org.myco.myproject.orgsclient.service.AbstractTransactionService.handleException(AbstractTransactionService.java:28)
    at org.myco.myproject.orgsclient.service.OrganizationServiceImpl.saveOrganization(OrganizationServiceImpl.java:42)
    at org.myco.myproject.orgsclient.service.OrganizationServiceTest.testSaveOrg(OrganizationServiceTest.java:40)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: org.hibernate.HibernateException: No CurrentSessionContext configured!
    at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:881)
    at org.myco.myproject.orgsclient.dao.OrganizationDAOImpl.findStateByAbbrev(OrganizationDAOImpl.java:29)
    at org.myco.myproject.orgsclient.service.OrganizationServiceImpl.saveOrganization(OrganizationServiceImpl.java:30)
    ... 24 more

您尚未指定Hibernate属性,该属性告诉
SessionFactory
使用
ThreadLocalSessionContext

setProperty("hibernate.current_session_context_class", "thread")
这意味着在构建
SessionFactory
时,Hibernate不会设置currentSessionContext。当您调用
getCurrentSession()
时,Hibernate无法从中检索当前会话


其他选项包括
jta
managed

您尚未指定Hibernate属性,该属性告诉
SessionFactory
使用
ThreadLocalSessionContext

setProperty("hibernate.current_session_context_class", "thread")
这意味着在构建
SessionFactory
时,Hibernate不会设置currentSessionContext。当您调用
getCurrentSession()
时,Hibernate无法从中检索当前会话


其他选项包括
jta
managed

向hibernate.cfg.xml文件添加以下属性:

<property name="hibernate.current_session_context_class">thread</property>
线程

将以下属性添加到hibernate.cfg.xml文件:

<property name="hibernate.current_session_context_class">thread</property>
线程