org.hibernate.SessionException:会话已关闭!错误

时间:2017-02-06 11:07:54

标签: hibernate

我通过传递id来删除对象,但它被删除但显示“会话已关闭”错误。我刚刚发布了这两个类,一个是控制器,另一个是删除方法,其中对象被删除但是说“会话已关闭”。

 In CategoryDAOImpl.java class

        /*
         * To change this license header, choose License Headers in Project Properties.
         * To change this template file, choose Tools | Templates
         * and open the template in the editor.
         */
        package com.acem.sp.dao.impl;

        import com.acem.sp.dao.CategoryDAO;
        import com.acem.sp.entity.Category;
        import java.util.List;
        import org.hibernate.Query;
        import org.hibernate.Session;
        import org.hibernate.SessionFactory;
        import org.hibernate.Transaction;
        import org.springframework.beans.factory.annotation.Autowired;


        import org.springframework.stereotype.Repository;

        /**
         *
         * @author AshwinKArki
         */
        @Repository
        public class CategoryDAOImpl implements CategoryDAO {

           @Autowired
            private SessionFactory sessionFactory;

            private Session session;

            private Transaction trans;
            @Override
            public List<Category> getAll() {
              session=sessionFactory.openSession();
            Query query=session.getNamedQuery("Category.findAll");
           List<Category> catList=query.list();
            session.close();
            return catList;
            }

            @Override
            public Category getById(int id) {
               session=sessionFactory.openSession();
          Category cat=(Category)session.get(Category.class, id);
           session.close();
            return cat;
                 }


            @Override
            public void insert(Category t) {
              session=sessionFactory.openSession();

                trans=session.beginTransaction();

                session.save(t);
                trans.commit();
                session.close();
            }

            @Override
            public void update(Category t) {
                session=sessionFactory.openSession();

                trans=session.beginTransaction();

                session.saveOrUpdate(t);
                trans.commit();
               session.close(); 
            }

            @Override
            public void delete(int id) {
               session = session.getSessionFactory().openSession();
                trans=session.beginTransaction();

             session.delete(getById(id));
             trans.commit();
              session.flush() ;
             session.close();


            }

        }
        In my admincontroller.java class

         @RequestMapping(value="/dashboard/delete/{id}",method=RequestMethod.GET)
            public String delete(@PathVariable("id") int id){
               categoryDAO.delete(id);
                return "admin/dashboard";
            }
        }

2 个答案:

答案 0 :(得分:0)

在这一行:

session.delete(getById(id));

在getById中,您正在关闭会话,当您执行删除时,会话已经关闭(理论上openSession创建了一个新会话但​​是,当前事务中已有一个,所以我猜它正在重用父母一个。)

解决方案是创建一个重载的getById方法,您可以将已经存在的会话传递给该方法并对其进行处理:

@Override
public Category getById(int id, Session session) {

     Category cat=(Category)session.get(Category.class, id);

     return cat;
}

在删除中你会:

@Override
public void delete(int id) {
      session = session.getSessionFactory().openSession();
      trans=session.beginTransaction();

      session.delete(getById(id, session));
      trans.commit();
      session.flush() ;
      session.close();
}

答案 1 :(得分:0)

您可以将@Transactional注释用于需要会话的方法

像这样:

   @Override
   @Transactional
   public void delete(int id) {
      session = session.getSessionFactory().openSession();
      trans=session.beginTransaction();

      session.delete(getById(id));
      trans.commit();
      session.flush() ;
      session.close();

        }