Pessoal, to implementando um CRUD aqui, e estou com o este problema:
org.hibernate.SessionException: Session is closed!
O Hibernate informa que a Session esta fechada, mas ao que vejo, não é o que acontece.
Tenho uma JSP que chama o metodo SaveCurso na Fachada, que chama o controlador e o DAOCurso(metodo insert).
Como no começo de cada metodo do DAO eu chamo o getSession(que abre sempre a Session) não sei pq ele informa que a Session está fechada.
Classe HibernateUtil
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package br.com.university.utils;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
/**
*
* @author Thiago
*/
public class HibernateUtil {
private static SessionFactory sessionFactory;
private static Session session;
static{
AnnotationConfiguration confs = new AnnotationConfiguration().configure();
sessionFactory = confs.buildSessionFactory();
}
public static Session getSession(){
session = sessionFactory.openSession();
session.beginTransaction();
return session;
}
public static void closeSession(){
session.close();
}
public static void commitTransaction(){
session.getTransaction().commit();
}
public static void closeSessionFactory(){
sessionFactory.close();
}
}
Classe DAOGeneric
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package br.com.university.data;
import br.com.university.utils.HibernateUtil;
import org.hibernate.Session;
/**
*
* @author Thiago
*/
public abstract class DAOGeneric {
Session getSession(){
return HibernateUtil.getSession();
}
void closeSession(){
HibernateUtil.getSession();
}
void commitTransaction(){
HibernateUtil.commitTransaction();
}
}
Classe DAOCurso
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package br.com.university.data;
import br.com.university.model.Curso;
import java.util.List;
/**
*
* @author Thiago
*/
public class DAOCurso extends DAOGeneric implements IDAOGeneric<Curso>{
public void remove(Integer pk) {
this.getSession().delete(this.search(pk));
this.closeSession();
}
public Curso search(Integer pk) {
Curso curso = (Curso) this.getSession().get(Curso.class, pk);
this.closeSession();
return curso;
}
public void edit(Curso entity) {
this.getSession().update(entity);
this.closeSession();
}
public void insert(Curso entity) {
this.getSession().save(entity);
this.commitTransaction();
this.closeSession();
}
public List<Curso> list(){
List<Curso> list = this.getSession().createQuery("from Curso curso").list();
this.commitTransaction();
this.closeSession();
return list;
}
}