81,095
社区成员
发帖
与我相关
我的任务
分享
Session session = sessionFactory.getCurrentSession(); //方法一
Session session = sessionFactory.openSession(); //方法二
session.close();
public static void main(String[] args) {
ApplicationContext con = new ClassPathXmlApplicationContext("applicationContext.xml");
StudentService studentService = (StudentService) con.getBean("studentServiceImpl");
List<ZtStudentLog> list = studentService.getTotalStudent();
for (ZtStudentLog z : list) {
System.out.println(z.getUsername()+" "+z.getAge()+" "+z.getId()+" "+z.getSex());
}
}
结果如下:Hibernate: select this_.ID as ID0_0_, this_.AGE as AGE0_0_, this_.SEX as SEX0_0_, this_.USERNAME as USERNAME0_0_ from ZT_STUDENT_LOG this_
王1 66 1 男
王2 55 2 女
王4 33 4 女
码子 13 5 女
码2 45 3 女
<!-- 上下文配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- springmvc配置 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
applicationContext.xml部分代码:
<context:component-scan base-package="com.pb" />
<!-- 读取jdbc.properties文件信息,方便更改数据库 -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:jdbc.properties</value>
</list>
</property>
</bean>
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!-- hibernate注释写法,不需要写*.hbm.xml了. -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${jdbc.hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${jdbc.hibernate.show_sql}</prop>
<prop key="hibernate.format_sql">${jdbc.hibernate.format_sql}</prop>
</props>
</property>
<property name="packagesToScan" value="com.pb.model." />
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" propagation="REQUIRED" read-only="true"/>
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<aop:config >
<aop:pointcut expression="execution(public * com.pb.service.impl.*.*(..))" id="pointCut"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pointCut"/>
</aop:config>
</beans>
springmvc-servlet.xml部分代码:
<!-- 默认的注解映射的支持 -->
<!-- <mvc:annotation-driven/> -->
<!-- 自动扫描的包名,对定义包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 -->
<context:component-scan base-package="com.pb" />
<!-- 视图解释类 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" /> <!--可为空,方便实现自已的依据扩展名来选择视图解释类的逻辑 -->
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<!-- 去掉了也能用JSTL.去掉了能用是因为JSTL:你加载了JSTL的标签架包。 -->
</bean>
</beans>
StudentDaoImpl.java部分代码:
@Repository
public class StudentDaoImpl implements StudentDao {
@Autowired
private SessionFactory sessionFactory;
@SuppressWarnings("unchecked")
@Override
public List<ZtStudentLog> getTotalStudent() {
Session session = sessionFactory.getCurrentSession(); //用这个会报错,线程中不存在session
// Session session = sessionFactory.openSession(); //用opensessoin不会报错,但不对数据库进行操作
List<ZtStudentLog> list = session.createCriteria(ZtStudentLog.class).list();
// session.close();
return list;
}
@Override
public void addStudent(ZtStudentLog ztStudentLog) {
// Session session = sessionFactory.getCurrentSession();
Session session = sessionFactory.openSession();
session.beginTransaction();
session.saveOrUpdate(ztStudentLog);
session.flush();
session.close();
}
}
StudentServiceImpl.java部分代码:
@Service()
public class StudentServiceImpl implements StudentService {
@Autowired
private StudentDao studentDao;
@Override
public List<ZtStudentLog> getTotalStudent() {
return studentDao.getTotalStudent();
}
@Override
public void addStudent(ZtStudentLog ztStudentLog) {
studentDao.addStudent(ztStudentLog);
}
}
testController.java部分代码:
@Controller
@RequestMapping("/test")
public class testController {
@Autowired
private StudentService studentService;
private List<ZtStudentLog> studentList;
public List<ZtStudentLog> getStudentList() {
return studentList;
}
public void setStudentList(List<ZtStudentLog> studentList) {
this.studentList = studentList;
}
@RequestMapping(value="/test0",method=RequestMethod.GET)
public String gettest0 (HttpServletRequest request) {
this.studentList = studentService.getTotalStudent();
request.setAttribute("studentList", studentList);
return "student_list";
}
@RequestMapping(value="/add0",method=RequestMethod.GET)
public String add0 () {
System.out.println("get进入======");
return "student_add";
}
}
student_list.jsp部分代码:<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'studnet_list.jsp' starting page</title>
</head>
<body>
<center>
${msg }
<table border="1">
<tr>
<td>编号</td>
<td>姓名</td>
<td>年龄</td>
<td>性别</td>
</tr>
<c:forEach var="student" items="${studentList}" varStatus="status">
<tr>
<td>${student.id }</td>
<td>${student.username }</td>
<td>${student.age }</td>
<td>${student.sex }</td>
</tr>
</c:forEach>
</table>
<a href="add0">添加信息</a>
</center>
</body>
</html>
报错详情:2014-8-20 12:17:06 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet springmvc threw exception
org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
at org.springframework.orm.hibernate3.SpringSessionContext.currentSession(SpringSessionContext.java:63)
at org.hibernate.impl.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:622)
at com.pb.dao.impl.StudentDaoImpl.getTotalStudent(StudentDaoImpl.java:21)
at com.pb.service.impl.StudentServiceImpl.getTotalStudent(StudentServiceImpl.java:20)
at com.pb.controller.testController.gettest0(testController.java:37)
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.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:228)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:634)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:445)
at java.lang.Thread.run(Thread.java:619)
<tx:annotation-driven transaction-manager="transactionManager" />
还是不行,后来换的申明式事务,但是我在service层直接跑数据是ok的,那是不是跟aop没关系啊?<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
serviceimpl层@Service()
@Transactional
public class StudentServiceImpl implements StudentService {
这个在service层连接数据库ok,但在controller层调用又报这个错了: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here<prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate3.SpringSessionContext</prop>
还是报错 No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here我要疯了,代码咋看咋对,就是从controller层调用就报错...<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${jdbc.hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${jdbc.hibernate.show_sql}</prop>
<prop key="hibernate.format_sql">${jdbc.hibernate.format_sql}</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
</props>
</property>
直接就跑不动了,报错 createCriteria is not valid without active transaction
第二次我用: current_session_context_class<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${jdbc.hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${jdbc.hibernate.show_sql}</prop>
<prop key="hibernate.format_sql">${jdbc.hibernate.format_sql}</prop>
<prop key="current_session_context_class">thread</prop>
</props>
</property>
这个在service层连接数据库ok,但在controller层调用又报这个错了: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here大神,请帮帮忙!<prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop>
如果还是出现这个问题的话,那么就是版本的问题了