2017-02-06 16 views
1

IDを渡してオブジェクトを削除していますが、削除されますが「セッションが閉じています」というエラーが表示されます。私はちょうどこの2つのクラスを1つはコントローラであり、もう1つはオブジェクトが削除されても "セッションが閉じている"と言うdeleteメソッドです。org.hibernate.SessionException:セッションが閉じられています!エラー

この行で
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"; 
      } 
     } 
+0

エラーログを投稿してください。 – Akshay

+0

org.springframework.web.util.NestedServletException:リクエスト処理に失敗しました。 org.hibernate.SessionException:セッションがすでに閉じられていた –

答えて

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(); 
} 
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(); 

     } 
関連する問題