org.springframework.web.util.NestedServletException和org.hibernate.HibernateException:找不到当前线程的会话

时间:2017-04-21 16:10:21

标签: java spring hibernate maven model-view-controller

我正在尝试使用spring hibernate将一些记录插入到我的数据库表中,但是在运行project之后会出现以下错误。请帮助我解决问题..谢谢

实体类Admin.java

package com.finalproject.entity;
// Generated Apr 20, 2017 4:49:19 PM by Hibernate Tools 4.3.1


import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import static javax.persistence.GenerationType.IDENTITY;
import javax.persistence.Id;
import javax.persistence.Table;

/**
 * Admin generated by hbm2java
 */
@Entity
@Table(name="admin"
    ,catalog="advertisement_system"
)
public class Admin  implements java.io.Serializable {


     private Integer adminId;
     private String adminName;
     private String adminEmail;
     private String adminPass;
     private byte[] adminPhoto;

    public Admin() {
    }

	
    public Admin(String adminName, String adminEmail, String adminPass) {
        this.adminName = adminName;
        this.adminEmail = adminEmail;
        this.adminPass = adminPass;
    }
    public Admin(String adminName, String adminEmail, String adminPass, byte[] adminPhoto) {
       this.adminName = adminName;
       this.adminEmail = adminEmail;
       this.adminPass = adminPass;
       this.adminPhoto = adminPhoto;
    }
   
     @Id @GeneratedValue(strategy=IDENTITY)

    
    @Column(name="admin_id", unique=true, nullable=false)
    public Integer getAdminId() {
        return this.adminId;
    }
    
    public void setAdminId(Integer adminId) {
        this.adminId = adminId;
    }

    
    @Column(name="admin_name", nullable=false, length=45)
    public String getAdminName() {
        return this.adminName;
    }
    
    public void setAdminName(String adminName) {
        this.adminName = adminName;
    }

    
    @Column(name="admin_email", nullable=false, length=45)
    public String getAdminEmail() {
        return this.adminEmail;
    }
    
    public void setAdminEmail(String adminEmail) {
        this.adminEmail = adminEmail;
    }

    
    @Column(name="admin_pass", nullable=false, length=45)
    public String getAdminPass() {
        return this.adminPass;
    }
    
    public void setAdminPass(String adminPass) {
        this.adminPass = adminPass;
    }

    
    @Column(name="admin_photo")
    public byte[] getAdminPhoto() {
        return this.adminPhoto;
    }
    
    public void setAdminPhoto(byte[] adminPhoto) {
        this.adminPhoto = adminPhoto;
    }
}

AdminServiceImpl.java类

package com.finalproject.service.impl;

import com.finalproject.dao.AdminDao;
import com.finalproject.entity.Admin;
import com.finalproject.service.AdminService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;


@Service("adminService")
@Transactional(propagation = Propagation.SUPPORTS,rollbackFor = Exception.class)
public class AdminServiceImpl implements AdminService {

    @Autowired
    AdminDao adminDao;

    @Override
    public void create(Admin admin) {
        adminDao.create(admin);
    }

    @Override
    public void update(Admin admin) {
        adminDao.update(admin);
    }

    @Override
    public void delete(Long adminId) {
        adminDao.delete(adminId);
    }

    @Override
    public Admin edit(Long adminId) {
        return adminDao.edit(adminId);
    }

    @Override
    public List<Admin> getAll() {
        return adminDao.getAll();
    }

    @Override
    public Admin find(Long adminId) {
        return adminDao.find(adminId);
    }

}

Dao实现了类AdminDaoImpl.java

package com.finalproject.dao.impl;

import com.finalproject.dao.AdminDao;
import com.finalproject.entity.Admin;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository("adminDao")
public class AdminDaoImpl implements AdminDao {

    @Autowired
    SessionFactory sessionFactory;
    

    protected Session currentSession() {
        return sessionFactory.getCurrentSession();
    }

    @Override
    public void create(Admin admin) {
        currentSession().save(admin);
    }

    @Override
    public void update(Admin admin) {
        currentSession().update(admin);
    }

    @Override
    public void delete(Long adminId) {
        currentSession().delete(adminId);
    }

    @Override
    public Admin edit(Long adminId) {
        return find(adminId);
    }

    @Override
    public Admin find(Long adminId) {
        return (Admin) currentSession().get(Admin.class, adminId);
    }

    @Override
    public List<Admin> getAll() {
        return currentSession().createCriteria(Admin.class).list();
    }

}

控制器类AdminController.java

package com.finalproject.controller;

import com.finalproject.entity.Admin;
import com.finalproject.service.AdminService;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping(value = "/admin")
public class AdminController {

    @Autowired
    AdminService adminService;

    @RequestMapping(value = "/register", method = RequestMethod.GET)
    public String register(Map<String, Object> map) {
        map.put("adminReg", new Admin());
        return "/admin/add";
    }

    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public String create(Admin admin, Map<String, Object> map) {
        adminService.create(admin);
        return "redirect:/admin/details/" + admin.getAdminId();
    }

    @RequestMapping(value = "/details/{adminId}",method = RequestMethod.GET)
    public String details(@PathVariable("adminId") Long adminId, Map<String, Object> map) {
        Admin admin = adminService.find(adminId);
        map.put("adminName", admin.getAdminName());
        map.put("adminEmail", admin.getAdminEmail());
        map.put("adminPass", admin.getAdminPass());

        return "admin/details";
    }

}

错误消息

type Exception report

message Request processing failed; nested exception is org.hibernate.HibernateException: No Session found for current thread

description The server encountered an internal error that prevented it from fulfilling this request.

exception

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.hibernate.HibernateException: No Session found for current thread
	org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:948)
	org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:838)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
	org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
	org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
	org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
	org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
root cause

org.hibernate.HibernateException: No Session found for current thread
	org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:97)
	org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:978)
	com.finalproject.dao.impl.AdminDaoImpl.currentSession(AdminDaoImpl.java:19)
	com.finalproject.dao.impl.AdminDaoImpl.create(AdminDaoImpl.java:24)
	com.finalproject.service.impl.AdminServiceImpl.create(AdminServiceImpl.java:22)
	sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	java.lang.reflect.Method.invoke(Method.java:498)
	org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
	org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
	org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
	org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:96)
	org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:260)
	org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:94)
	org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
	org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
	com.sun.proxy.$Proxy125.create(Unknown Source)
	com.finalproject.controller.AdminController.create(AdminController.java:29)
	sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	java.lang.reflect.Method.invoke(Method.java:498)
	org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:219)
	org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
	org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
	org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:745)
	org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:686)
	org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
	org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
	org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
	org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936)
	org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:838)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
	org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
	org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
	org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
	org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)

Error Message AdminDaoImpl.java

0 个答案:

没有答案