Why can’t I erase an object with Hibernate?

Asked

Viewed 383 times

0

I have on a screen a process table and want to delete one of them. This is a piece of mine datatable:

<p:column headerText="Excluir" style="width:68px">
    <p:commandButton actionListener="#{processoMB.excluir(obj)}"
        icon="ui-icon-trash" title="Excluir" styleClass="btn btn-block"
        update=":frmTabela :dlgInformacao" alt="Excluir">
        <p:confirm header="Confirmação" message="Deseja excluir?" />
    </p:commandButton>
</p:column>

This is my method exclude(Procedure):

public void excluir(Processo processo) {
    try {
        daoProcesso.remover(processo);
        ExibirMensagem.exibirMensagem(Mensagem.SUCESSO);
        operacoesDeAlteracao();
    } catch (Exception e) {
        ExibirMensagem.exibirMensagem(Mensagem.ERRO);
        e.printStackTrace();
    }
}

And this is my method remove(T object) in Genericdao

public void remover(T objeto) {
    Object  o = manager.merge(objeto);
    manager.remove(o);
}

When I click on the button to delete a process Hibernate does only these operations and does not delete my process object:

Hibernate: select processo0_.id_processo as id_proce1_0_0_, processo0_.descricao as descrica2_0_0_ from tab_processo processo0_ where processo0_.id_processo=?
Hibernate: select processo0_.id_processo as id_proce1_0_, processo0_.descricao as descrica2_0_ from tab_processo processo0_ where id_processo<>0

What am I doing wrong?

  • Select the object after use remove. Don’t forget to open and close a transaction.

  • I don’t know if I understood how to select an object, take a look at line 44 and 45 of that code to see if I did it right. As to open and close the transaction this is being done by a third party with CDI.

  • It would look something like this: Object o = manager.find(Object.class, id). Eu uso CDI e no DAO eu uso a annotation @Transactional`.

  • I tried that, but it still doesn’t work. That one is the new method I created.

  • Put a method that exists and works so that we can compare. In this case if you pass the object as parameter, I believe it is not necessary manager.merge. Could use the method remove' directly. I use the "Weld" implementation and created a class to assist transactions and use the annotation @Transaction in each method that updates or removes objects from the BD.

  • Since you are making a change on a bank line it is necessary to open the transaction and after the change commit the change. Where do you do it?

  • It may not have much to do.. But it’s worth trying: instead of doing Object o try to do T o.

  • @Marcelogomes I still don’t understand very well how the CDI works, but I had a class in my project called Transacional. I put the note @Transacional in my method remover(Class<T> classe, Long id) and now I can finally delete objects.

  • I will add this answer and you mark as the correct answer. Who good it worked.

Show 4 more comments

2 answers

0

   try {

           public void remover(T objeto) {
            manager.getTransaction().begin() ;
            Object  o = manager.merge(objeto);
            manager.remove(o);
            manager.getTransaction().commit();
         }

 } catch (Exception ex) {
    ex.printStackTrace();                          
    manager.getTransaction().rollback();                         
 }

You can try another option!

manager.remove( manager.find(seuObjeto.class, id) );

If the other option doesn’t work try yes! if you have a method that searches for id you can use it to replace the find manager.().

0


Using CDI you should annotate the method in DAO or Service, depending on your strategy:

@Transacional 
public void excluir(Processo processo) {
    try {
        daoProcesso.remover(processo);
        ExibirMensagem.exibirMensagem(Mensagem.SUCESSO);
        operacoesDeAlteracao();
    } catch (Exception e) {
        ExibirMensagem.exibirMensagem(Mensagem.ERRO);
        e.printStackTrace();
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.