我正在开发 Spring MVC 项目。我直接使用休眠。No Session found for current thread
我的项目中发生了错误。我知道在 stackoverflow 和网络上的其他地方,这个错误很常见。但我还没有找到解决我问题的有效方法。
我在正确配置 Hibernate 会话工厂时遇到了未解决的问题。我在这里进行了描述:我可以使用在 DispatcherServlet Context 中声明的 Hibernate Session Factory 而不是 hibernate.cfg.xml?但最后我使用的是hibernate.cfg.xml
文件以及在 DispatcherServlet 上下文文件中定义的 Hibernate Session Factory。我做了一些简单的操作,比如在数据库中持久化类。一切都很好。
目前我的项目与Spring MVC Hibernate有很多共同点。我为我的类添加了一些注释,并创建了一些新的服务类。
我的 ServletDispatcher 上下文文件包含以下定义:
<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);
No Session found for current thread
调用addUser()
方法时出现问题。
我的 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>
编辑#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>