Estou tendo o seguinte erro ao tentar deletar qualquer objeto que eu tenha.
Nas classes eu estou usando generator class=“increment” para todas, e o objeto eu estou pegando de um ListDataModel do JSF, mas não acho que isso é relevante, afinal eu consigo alterar e cadastrar um novo. O erro ocorre apenas na hora de deletar.
Meu DAO:
public abstract class GenericDAO<T> extends HibernateUtil {
public void excluir(T t) throws Exception {
getSession();
begin();
getSession().delete(t);
commit();
flush();
getSession().clear();
}
}
HibernateUtil
public class HibernateUtil {
private static SessionFactory sessionFactory;
private static ThreadLocal threadlocal = new ThreadLocal();
public static Session getSession(){
Session session = (Session) threadlocal.get();
if(session == null){
session = sessionFactory.openSession();
threadlocal.set(session);
}
return session;
}
protected void begin(){
getSession().beginTransaction();
}
protected void commit(){
getSession().getTransaction().commit();
}
protected void flush(){
getSession().flush();
}
protected void rollback(){
getSession().getTransaction().rollback();
}
protected void close(){
getSession().close();
}
protected static void shutDown(){
getSessionFactory().close();
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
Também criei um filtro, pois antes estava dando LazyInitializationException.
public class HibernateSessionRequestFilter implements Filter{
private SessionFactory sf;
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
try {
sf.getCurrentSession().beginTransaction();
chain.doFilter(request, response);
sf.getCurrentSession().getTransaction().commit();
}catch (StaleObjectStateException staleEx){
throw staleEx;
}catch (Throwable ex) {
try {
if (sf.getCurrentSession().getTransaction().isActive()) {
sf.getCurrentSession().getTransaction().rollback();
}
} catch (Throwable rbEx) {
}
// Let others handle it... maybe another interceptor for exceptions?
throw new ServletException(ex);
}
}
@Override
public void init(FilterConfig filterConfig) throws ServletException {
sf = HibernateUtil.getSessionFactory();
}
@Override
public void destroy() {
// TODO Auto-generated method stub
}