更改访问方法后,“当前线程找不到会话”

4
我正在开发Spring MVC项目,直接使用Hibernate。在我的项目中出现了错误“当前线程没有找到会话(No Session found for current thread)”。我知道这个错误在stackoverflow和其他网站上很常见,但我没有找到适用于我的问题的解决方案。
我一直无法解决Hibernate Session Factory的正确配置问题,我在这里描述了它:我可以使用在DispatcherServlet Context中声明的Hibernate Session Factory代替hibernate.cfg.xml文件吗? 但最终我仍然使用了hibernate.cfg.xml文件,并且还定义了在DispatcherServlet Context文件中的Hibernate Session Factory。我执行了一些简单的操作,如将类保存到数据库中,一切都正常。
目前,我的项目与Spring MVC Hibernate有许多共同点。我已经为我的类添加了一些注释,并创建了一些新的服务类。
我的ServletDispatcher Context文件包含以下定义:
<annotation-driven />
<context:annotation-config />
<context:component-scan base-package="finances.webapp" />

<!-- Data source -->
<beans:bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <beans:property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <beans:property name="url" value="jdbc:mysql://localhost/finances" />
    <beans:property name="username" value="root" />
    <beans:property name="password" value="root" />
</beans:bean>
<!-- Data source end -->

<!-- Hibernate session factory -->
<beans:bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <beans:property name="dataSource">
          <beans:ref bean="dataSource" />
    </beans:property>
    <beans:property name="configLocation" value="classpath:hibernate.cfg.xml" />
</beans:bean>
<!-- Hibernate session factory end -->

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

<beans:bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
    <beans:property name="transactionManager" ref="transactionManager" />
    <beans:property name="transactionAttributes">
        <beans:props>
            <beans:prop key="save">PROPAGATION_REQUIRED</beans:prop>
        </beans:props>
    </beans:property>
</beans:bean>

在我的pom.xml文件中,我只有这两个与ORM相关的依赖项:
<!-- Hibernate -->
<dependency>
   <groupId>org.hibernate</groupId>
   <artifactId>hibernate-entitymanager</artifactId>
   <version>4.1.4.Final</version>
</dependency>

<!-- EJB -->
<dependency>
   <groupId>javax.ejb</groupId>
   <artifactId>ejb-api</artifactId>
   <version>3.0</version>
   <scope>provided</scope>
</dependency>

也许问题就出在这里?但是之前我没有遇到任何缺少软件包的问题。
这是我的控制器代码:
UsersEntity user2 = new UsersEntity("qwerty", "qwerty", true);
usersService.addUser(user2);

调用addUser()方法时出现了问题No Session found for current thread

我的DAO类:

@Repository
public class UsersHome {

    @Autowired
    private SessionFactory sessionFactory;    

    public void persist(UsersEntity transientInstance) {
        sessionFactory.getCurrentSession().persist(transientInstance);
    }
}

我的服务:

@Service
public class UsersService {

    @Autowired
    private UsersHome usersHome;

    @Transactional(propagation = Propagation.REQUIRED)
    public void addUser(UsersEntity user) {
        usersHome.persist(user);
    }
}

当我更改UsersHome类的方法体时,这段代码就停止工作了。之前我一直在我的操作之前启动会话并在我的操作之后结束会话。但是使用注释和事务,它应该能够工作吗?请给我一些建议。
编辑:
<hibernate-configuration>
    <session-factory>
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="show_sql">true</property>
        <property name="hbm2ddl.auto">update</property>
        <property name="connection.url">jdbc:mysql://localhost/finances</property>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.username">root</property>
        <property name="connection.password">root</property>
        <mapping class="finances.webapp.entities.AuthoritiesEntity"/>
        <mapping class="finances.webapp.entities.ExpensesEntity"/>
        <mapping class="finances.webapp.entities.ExpensesCategoriesEntity"/>
        <mapping class="finances.webapp.entities.ExpensesObjectsEntity"/>
        <mapping class="finances.webapp.entities.IncomesEntity"/>
        <mapping class="finances.webapp.entities.UsersEntity"/>
    </session-factory>
</hibernate-configuration>

EDIT #2

org.springframework.beans.factory.BeanCreationException: Error creating bean with 
name 'usersService': Injection of autowired dependencies failed; nested exception 
is org.springframework.beans.factory.BeanCreationException: Could not autowire 
field: private finances.webapp.dao.UsersHome 
finances.webapp.services.UsersService.usersHome;
nested exception is org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'usersHome': Injection of autowired dependencies 
failed; nested exception is org.springframework.beans.factory.BeanCreationException:
Could not autowire field: private org.hibernate.SessionFactory
finances.webapp.dao.UsersHome.sessionFactory; nested exception is 
org.springframework.beans.factory.BeanCreationException: Error creating bean with
name 'sessionFactory' defined in ServletContext resource 
[/WEB-INF/spring/appServlet/servlet-context.xml]: Invocation of init method 
failed; nested exception is java.lang.NoClassDefFoundError: 
org/hibernate/cfg/EJB3DTDEntityResolver

编辑 #3

我的root-context.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<!-- Root Context: defines shared resources visible to all other web components -->

</beans>

以下是我的整个spring-config.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns:beans="http://www.springframework.org/schema/beans"
         xmlns:context="http://www.springframework.org/schema/context"
         xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
                    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />
<context:annotation-config />
<context:component-scan base-package="finances.webapp" />

<!-- Handles HTTP GET requests for resources by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />

<!--<beans:bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">-->
    <!--<beans:property name="definitions">-->
        <!--<beans:list>-->
            <!--<beans:value>/WEB-INF/tiles-definitions.xml</beans:value>-->
        <!--</beans:list>-->
    <!--</beans:property>-->
<!--</beans:bean>-->

<!--<beans:bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">-->
    <!--<beans:property name="viewClass" value="org.springframework.web.servlet.view.tiles2.TilesView" />-->
<!--</beans:bean>-->

<!-- Data source -->
<beans:bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <beans:property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <beans:property name="url" value="jdbc:mysql://localhost/finances" />
    <beans:property name="username" value="root" />
    <beans:property name="password" value="root" />
</beans:bean>
<!-- Data source end -->

<!-- Hibernate session factory -->
<beans:bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <beans:property name="dataSource" ref="dataSource" />
    <beans:property name="configLocation" value="classpath:hibernate.cfg.xml" />
</beans:bean>
<!-- Hibernate session factory end -->

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

<beans:bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
    <beans:property name="transactionManager" ref="transactionManager" />
    <beans:property name="transactionAttributes">
        <beans:props>
            <beans:prop key="save">PROPAGATION_REQUIRED</beans:prop>
            <beans:prop key="persist">PROPAGATION_REQUIRED</beans:prop>
        </beans:props>
    </beans:property>
</beans:bean>

</beans:beans>

请查看此链接:https://dev59.com/HW855IYBdhLWcg3whExL - axtavt
UsersService 的连线在哪里? - Santosh
@Santosh:我的控制器中已连接了 UsersService - woyaru
@axtavt:我的配置文件中没有类似的属性。 - woyaru
@woyaru,你的hibernate.properties文件中有什么内容?你是否定义了属性hibernate.current_session_context_class? - user1516873
@user1516873:我已经编辑了我的问题。我添加了我的Hibernate配置文件。我没有定义这个属性。 - woyaru
1个回答

2
尝试移除 "transactionInterceptor",因为你已经设置了 @Transaction 属性并且添加了标签 <annotation-driven/>。或者为 persist() 方法添加 transactionInterceptor 键。
<beans:prop key="persist">PROPAGATION_REQUIRED</beans:prop>

也许它会有所帮助。
编辑: 我重新创建了您的配置,现在可以正常工作了。
这是Spring配置:
<?xml  version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:jdbc="http://www.springframework.org/schema/jdbc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/jdbc
        http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
        ">

    <context:annotation-config />
    <context:component-scan base-package="finances.webapp" />


    <jdbc:embedded-database id="dataSource" type="H2"/>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="configLocation" value="classpath:hibernate.cfg.xml" />
    </bean>

    <tx:annotation-driven />

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

</beans>

这是Hibernate配置:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="dialect">org.hibernate.dialect.H2Dialect</property>
        <property name="show_sql">true</property>
        <property name="hbm2ddl.auto">update</property>
        <mapping class="finances.webapp.bean.UsersEntity"/>
    </session-factory>
</hibernate-configuration>

pom.xml 中的依赖

<properties>
        <org.springframework.version>3.1.0.RELEASE</org.springframework.version>
        <org.hibernate.version>4.1.4.Final</org.hibernate.version>
    </properties>
  <dependencies>
      <dependency>
          <groupId>org.hibernate</groupId>
          <artifactId>hibernate-entitymanager</artifactId>
          <version>${org.hibernate.version}</version>
      </dependency>
      <dependency>
          <groupId>org.hibernate</groupId>
          <artifactId>hibernate-core</artifactId>
          <version>${org.hibernate.version}</version>
      </dependency>

      <dependency>
          <groupId>org.hibernate</groupId>
          <artifactId>hibernate-annotations</artifactId>
          <version>3.5.6-Final</version>
      </dependency>

      <dependency>
          <groupId>javax.ejb</groupId>
          <artifactId>ejb-api</artifactId>
          <version>3.0</version>
          <scope>provided</scope>
      </dependency>
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-core</artifactId>
          <version>${org.springframework.version}</version>
      </dependency>
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context</artifactId>
          <version>${org.springframework.version}</version>
      </dependency>
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-tx</artifactId>
          <version>${org.springframework.version}</version>
      </dependency>
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-jdbc</artifactId>
          <version>${org.springframework.version}</version>
      </dependency>
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-orm</artifactId>
          <version>${org.springframework.version}</version>
      </dependency>
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-aop</artifactId>
          <version>${org.springframework.version}</version>
      </dependency>
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-webmvc</artifactId>
          <version>${org.springframework.version}</version>
      </dependency>
      <dependency>
          <groupId>cglib</groupId>
          <artifactId>cglib</artifactId>
          <version>2.2.2</version>
      </dependency>


      <!--for test only -->
      <dependency>
          <groupId>com.h2database</groupId>
          <artifactId>h2</artifactId>
          <version>1.3.168</version>
      </dependency>

      <dependency>
          <groupId>org.testng</groupId>
          <artifactId>testng</artifactId>
          <version>6.1.1</version>
          <scope>test</scope>
      </dependency>

单元测试:

public class TestPersist {
    @org.testng.annotations.Test
    public void testPersistence() throws Exception {

        ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-context.xml");
        Assert.assertNotNull(ctx);

        UsersService userService = ctx.getBean(UsersService.class);

        Assert.assertNotNull(userService);
        UsersEntity user2 = new UsersEntity();
        user2.setEnable(true);
        user2.setLogin("querty");
        user2.setName("user2");

        userService.addUser(user2);

    }
}

以及单元测试输出:

Sep 8, 2012 2:55:37 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@5d3ad33d: startup date [Sat Sep 08 14:55:37 MSK 2012]; root of context hierarchy
Sep 8, 2012 2:55:37 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [spring-context.xml]
Sep 8, 2012 2:55:37 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@6cb32ed4: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,usersHome,usersService,dataSource,sessionFactory,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0,org.springframework.transaction.interceptor.TransactionInterceptor#0,org.springframework.transaction.config.internalTransactionAdvisor,transactionManager,org.springframework.context.annotation.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor#0]; root of factory hierarchy
Sep 8, 2012 2:55:37 PM org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseFactory initDatabase
INFO: Creating embedded database 'dataSource'
Sep 8, 2012 2:55:38 PM org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.1.Final}
Sep 8, 2012 2:55:38 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.1.4.Final}
Sep 8, 2012 2:55:38 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Sep 8, 2012 2:55:38 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Sep 8, 2012 2:55:38 PM org.hibernate.cfg.Configuration configure
INFO: HHH000044: Configuring from URL: file:/home/gr/dev/target/classes/hibernate.cfg.xml
Sep 8, 2012 2:55:38 PM org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
Sep 8, 2012 2:55:38 PM org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
Sep 8, 2012 2:55:38 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.H2Dialect
Sep 8, 2012 2:55:38 PM org.hibernate.engine.jdbc.internal.LobCreatorBuilder useContextualLobCreation
INFO: HHH000423: Disabling contextual LOB creation as JDBC driver reported JDBC version [3] less than 4
Sep 8, 2012 2:55:38 PM org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService
INFO: HHH000399: Using default transaction strategy (direct JDBC transactions)
Sep 8, 2012 2:55:38 PM org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
INFO: HHH000397: Using ASTQueryTranslatorFactory
Sep 8, 2012 2:55:38 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000228: Running hbm2ddl schema update
Sep 8, 2012 2:55:38 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000102: Fetching database metadata
Sep 8, 2012 2:55:38 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000396: Updating schema
Sep 8, 2012 2:55:38 PM org.hibernate.tool.hbm2ddl.DatabaseMetadata getTableMetadata
INFO: HHH000262: Table not found: UsersEntity
Sep 8, 2012 2:55:38 PM org.hibernate.tool.hbm2ddl.DatabaseMetadata getTableMetadata
INFO: HHH000262: Table not found: UsersEntity
Sep 8, 2012 2:55:39 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000232: Schema update complete
Sep 8, 2012 2:55:39 PM org.springframework.orm.hibernate4.HibernateTransactionManager afterPropertiesSet
INFO: Using DataSource [org.springframework.jdbc.datasource.SimpleDriverDataSource@79ef3ccd] of Hibernate SessionFactory for HibernateTransactionManager
Hibernate: insert into UsersEntity (id, enable, login, name) values (null, ?, ?, ?)
RemoteTestNG finishing: 2868 ms

谢谢您的回答。在我发布问题之前,我已经检查了没有transactionInterceptor的配置,所以这不是解决方案。明天我会尝试添加persist()的关键字并写下结果。 - woyaru
我已经为 persist() 方法添加了键值,但错误仍然存在。你有其他的建议吗? - woyaru
1
实际上,我认为配置看起来很好。我会尝试在我的本地环境中重新创建您的配置,并找到可行的解决方案。 - user1516873
当您使用不同的Hibernate版本时,会出现此异常。我建议暂时切换应用程序中使用与Jboss相同的Hibernate版本。第二种选择是不将Hibernate Jars部署到应用程序中。 - user1516873
1
在您的context-xml中,默认命名空间为xmlns="http://www.springframework.org/schema/mvc"。添加tx命名空间xmlns:tx="http://www.springframework.org/schema/tx"并将行<annotation-driven />更改为<tx:annotation-driven />。 - user1516873
显示剩余19条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接