我在 @Service 层加上 @Transactional 标签提示我 获取不到 session
这里是 applicationContext.xml
Service
这是同样的代码 从家里拷到公司. 家里没问题. 但是在公司电脑 @Transactional加在Service 层就获取不到
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:988)
at org.framework.core.orm.support.MapperImpl.query(MapperImpl.java:110)
at org.framework.core.orm.dao.support.AbstractBaseDaoImpl.query(AbstractBaseDaoImpl.java:23)
at org.framework.core.orm.dao.support.CommonDao.queryEntityList(CommonDao.java:49)
at org.framework.core.orm.dao.support.CommonDao.queryEntityList(CommonDao.java:39)
at org.framework.web.controller.service.ServeService.getCitys(ServeService.java:38)
at org.framework.web.controller.ServiceController.getUserList(ServiceController.java:26)
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.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:440)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:428)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:827)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)
这里是 applicationContext.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"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd ">
<context:annotation-config />
<!-- 自动扫描所有注解该路径 -->
<context:component-scan base-package="org.framework" />
<context:property-placeholder location="classpath:hibernate.properties" />
<!-- DataSource Start -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${dataSource.driverClassName}" />
<property name="url" value="${dataSource.url}" />
<property name="username" value="${dataSource.username}" />
<property name="password" value="${dataSource.password}" />
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 扫描映射文件,实体类 -->
<property name="packagesToScan">
<list>
<value>org.framework.define.bean.entity</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${dataSource.dialect}</prop>
<prop key="hibernate.hbm2ddl.auto">${dataSource.hbm2ddl.auto}</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<!-- DataSource End -->
<!-- Transaction Start -->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<!-- Transaction End -->
</beans>
Service
package org.framework.web.controller.service;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.framework.core.orm.dao.ICommonDao;
import org.framework.define.bean.entity.City;
import org.framework.define.bean.entity.Student;
import org.framework.define.bean.result.Data;
import org.framework.define.bean.result.DataResult;
import org.framework.define.bean.result.Page;
import org.framework.define.bean.result.Result;
import org.framework.define.exception.error.support.Errors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
@Transactional
public class ServeService {
@Autowired
private ICommonDao commonDao;
public Result getCitys(HttpServletRequest request, Map<String,String> webParams){
int page = webParams.get("page")==null?1:Integer.parseInt(webParams.get("page"));
Page iPage = new Page();
iPage.setPage(page);
//List<Map<String, Object>> mapList = commonDao.queryMap("select * from city where name like ?", new Object[]{"A%"}, (page-1)*iPage.getNum(), iPage.getNum());
List<Student> list = (List<Student>) commonDao.queryEntityList("select * from student", Student.class, (page-1)*iPage.getNum(), iPage.getNum());
int totalnum = commonDao.count("select count(0) from student");
iPage.setTotalnum(totalnum);
return new DataResult(Errors.OK, new Data(list, iPage));
}
}
这是同样的代码 从家里拷到公司. 家里没问题. 但是在公司电脑 @Transactional加在Service 层就获取不到