2

I have a simple delete method, it gets the id from a table that lists all from the db:

@RequestMapping(value = "/personagem/excluir.htm") public ModelAndView doExcluir(@RequestParam("id") int id) { logger.info("Recebendo parametro: {Id => " + id + "}"); try { Personagem p = getPersonagemService().buscarPorId(id); logger.info("Excluindo personagem: {Id => " + p.getId() + ", Nome => " + p.getNome() + ", Classe => " + p.getClasse() + "}"); getPersonagemService().excluir(p); return new ModelAndView("excluir"); } catch (Exception ex) { return new ModelAndView("erro", "mensagem", "Ocorreu um erro: " + ex); } } 

And heres the delete method from my dao:

public void excluir(Object obj) { getSession().beginTransaction(); getSession().delete(obj); getSession().flush(); getSession().close(); } 

I click the delete button, that call for the doExcluir() method, that should delete the data. It comes back to the list page, as it should, but it just wont remove the data from the table. Did i do anything wrong?

3 Answers 3

2

You're not committing the transaction:

public void excluir(Object obj) { getSession().beginTransaction(); getSession().delete(obj); getSession().getTransaction().commit(); getSession().close(); }

As a side note, why don't you use Spring and its integrated transaction management to remove all this boilerplate code?

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you xD i dunno how to do that with spring, the way i did is the only way i know =/ Note: i did what u told and now i get this: org.hibernate.TransactionException: Transaction not successfully started)
1

You can take the Spring's pet-clinic example as a reference. I included relevant parts for your case:

 public class HibernateClinic implements Clinic { private SessionFactory sessionFactory; @Autowired public HibernateClinic(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } public void deletePet(int id) throws DataAccessException { Pet pet = loadPet(id); sessionFactory.getCurrentSession().delete(pet); } } 

1 Comment

I noticed my getSession() method was getting a new session everytime i called it, so on commit, it was committing a different session (i think). I just added: Session session = getSession();, used session instead of getSession() and it worked just fine xD
0

You can add @Transactional to your controller method.

Of course you need to configure Spring to recognize this. But then you can skip a lot of the session an transaction handling code.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.