Reputation: 385
I am deleting an object by passing id but it gets deleted but shows "session is closed" error. I have just posted this two classes one is controller and another is delete method where the object gets deleted but says "session is closed".
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";
}
}
Upvotes: 1
Views: 5391
Reputation: 103
You can use @Transactional annotation to the methods that need a session
Like this:
@Override
@Transactional
public void delete(int id) {
session = session.getSessionFactory().openSession();
trans=session.beginTransaction();
session.delete(getById(id));
trans.commit();
session.flush() ;
session.close();
}
Upvotes: 0
Reputation: 26522
On this line:
session.delete(getById(id));
Inside the getById, you are closing the session and when you do the delete the session is already closed (in theory the openSession
creates a new session but, there is already one in the current transaction, so i guess it is reusing the parent one).
A solution would be to create an overloaded getById method to which you can pass an already existing session and work on that:
@Override
public Category getById(int id, Session session) {
Category cat=(Category)session.get(Category.class, id);
return cat;
}
And in the delete you would:
@Override
public void delete(int id) {
session = session.getSessionFactory().openSession();
trans=session.beginTransaction();
session.delete(getById(id, session));
trans.commit();
session.flush() ;
session.close();
}
Upvotes: 1