我正在尝试使用 Spring3 MVC 和 Hibernate 制作应用程序,但是我做错了,因为每次我尝试进行数据库查询时都会出现此错误:
org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
org.springframework.orm.hibernate3.SpringSessionContext.currentSession(SpringSessionContext.java:63)
org.hibernate.impl.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:687)
com.ibex.pBaseSpring.repository.hibernate.HibernateUsuarioDAO.login(HibernateUsuarioDAO.java:25)
com.ibex.pBaseSpring.service.impl.UsuarioManagerImpl.login(UsuarioManagerImpl.java:23)
com.ibex.pBaseSpring.web.LoginController.login(LoginController.java:32)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:601)
org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:427)
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:415)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:788)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:717)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560)
javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
我尝试了所有找到的解决方案,但仍然出现相同的错误,这是我的代码:
应用程序配置.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:mvc="http://www.springframework.org/schema/mvc"
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/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<context:component-scan base-package="com.ibex.pBaseSpring.repository" />
<context:component-scan base-package="com.ibex.pBaseSpring.service" />
<context:component-scan base-package="com.ibex.pBaseSpring.web" />
<context:annotation-config />
<mvc:annotation-driven />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView"></property>
<property name="prefix" value="/WEB-INF/views/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<context:property-placeholder location="/WEB-INF/spring/hibernate.properties" />
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName">
<value>${hibernate.connection.driver_class}</value>
</property>
<property name="url">
<value>${hibernate.connection.url}</value>
</property>
<property name="username">
<value>${hibernate.connection.username}</value>
</property>
<property name="password">
<value>${hibernate.connection.password}</value>
</property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.ibex.pBaseSpring.domain" />
<property name="hibernateProperties">
<props>
<prop key="dialect">org.hibernate.dialect.MySQLDialect</prop>
</props>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<mvc:resources location="/resources/" mapping="/resources/**" />
</beans>
web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>pBaseSpring</display-name>
<servlet>
<servlet-name>pBaseSpring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/app-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>pBaseSpring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
登录控制器
package com.ibex.pBaseSpring.web;
import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.ibex.pBaseSpring.domain.Usuario;
import com.ibex.pBaseSpring.service.UsuarioManager;
@Controller
public class LoginController {
private UsuarioManager usuarioManager;
@Inject
public LoginController(UsuarioManager usuarioManager){
this.usuarioManager = usuarioManager;
}
@RequestMapping(value="/inicio")
public String inicio(){
//comprobar que el usuario no esta registrado ya...
return "login";
}
@RequestMapping(value="/login")
public String login(@RequestParam("login") String login, @RequestParam("pass") String pass){
System.out.println(login + " - " + pass);
Usuario usuario = usuarioManager.login(login, pass);
return "main";
}
@RequestMapping(value="/logout")
public String logout(){
return "inicio";
}
}
UsuarioManager
package com.ibex.pBaseSpring.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ibex.pBaseSpring.domain.Usuario;
import com.ibex.pBaseSpring.repository.UsuarioDAO;
import com.ibex.pBaseSpring.service.UsuarioManager;
@Service
public class UsuarioManagerImpl implements UsuarioManager{
@Autowired
private UsuarioDAO usuarioDAO;
public void setUsuarioDAO(UsuarioDAO usuarioDAO) {
this.usuarioDAO = usuarioDAO;
}
public Usuario login(String login, String pass) {
return usuarioDAO.login(login, pass);
}
}
HibernateUsuarioDAO
package com.ibex.pBaseSpring.repository.hibernate;
import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import com.ibex.pBaseSpring.domain.Usuario;
import com.ibex.pBaseSpring.repository.UsuarioDAO;
@Repository
public class HibernateUsuarioDAO implements UsuarioDAO{
private SessionFactory sessionFactory;
@Autowired
public HibernateUsuarioDAO(SessionFactory sessionFactory){
this.sessionFactory = sessionFactory;
}
@Transactional(readOnly = true)
public Usuario login(String login, String pass) {
Criteria c = sessionFactory.getCurrentSession().createCriteria(Usuario.class);
c.add(Restrictions.eq("login", login));
c.add(Restrictions.eq("pass", pass));
return (Usuario) c.uniqueResult();
}
}
我有点绝望,谢谢你的帮助。再见!!