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

我正在开发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上下文文件包含以下定义:

                           PROPAGATION_REQUIRED    

在我的pom.xml文件中,根据ORM,我只有这两个依赖项:

   org.hibernate hibernate-entitymanager 4.1.4.Final    javax.ejb ejb-api 3.0 provided  

也许问题在这里? 但是我之前没有丢包的问题。

这是我的控制器的代码:

 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类中更改方法体时,此代码已停止工作。 以前我在行动之前开始会议,在我的行动结束后开始会议。 但是使用注释和交易应该有用吗? 请给我一些建议。

编辑

   org.hibernate.dialect.MySQLDialect true update jdbc:mysql://localhost/finances com.mysql.jdbc.Driver root root         

编辑#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文件:

     

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

          <!----> <!----> <!----> <!--/WEB-INF/tiles-definitions.xml--> <!----> <!----> <!----> <!----> <!----> <!---->                      PROPAGATION_REQUIRED PROPAGATION_REQUIRED     

尝试删除“transactionInterceptor”,您已经设置了@Transaction属性,并且还有标记 。 或者为persist()方法添加到transactionInterceptor键

 PROPAGATION_REQUIRED 

也许它有帮助。

编辑:我重新创建了您的配置,它的工作原理。

这个spring配置:

               

这是hibernate配置:

     org.hibernate.dialect.H2Dialect true update    

pom.xml中的依赖项

  3.1.0.RELEASE 4.1.4.Final    org.hibernate hibernate-entitymanager ${org.hibernate.version}   org.hibernate hibernate-core ${org.hibernate.version}   org.hibernate hibernate-annotations 3.5.6-Final   javax.ejb ejb-api 3.0 provided   org.springframework spring-core ${org.springframework.version}   org.springframework spring-context ${org.springframework.version}   org.springframework spring-tx ${org.springframework.version}   org.springframework spring-jdbc ${org.springframework.version}   org.springframework spring-orm ${org.springframework.version}   org.springframework spring-aop ${org.springframework.version}   org.springframework spring-webmvc ${org.springframework.version}   cglib cglib 2.2.2    com.h2database h2 1.3.168   org.testng testng 6.1.1 test  

unit testing:

 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); } } 

和unit testing输出:

 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  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  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  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  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