How / When to Close Hibernate Session?

5

I have an error after closing Hibernate Session

public String listarTodosClientes() {
    session.getTransaction().begin();
    List<Cliente> lista = session.createCriteria(Cliente.class).list();
    session.close();
    return lista.toString();
}

After session.close (), the toString () method generates an Exception. But if I remove the session.close (). The code works. And now I can leave the session open?

p.s: return a string was just an example. could be returning some item from the list.

    
asked by anonymous 13.11.2014 / 13:36

1 answer

3

RESOLVED.

Solution!

public String listarTodosClientes() {
    session.getTransaction().begin();
    List<Cliente> lista = session.createCriteria(Cliente.class).list();
    String retorno = lista.toString();
    session.close();
    return retorno;
}
    
13.11.2014 / 14:06