exclusion of a database object with jpa 2 and jsf 2

Asked

Viewed 1,726 times

2

Well I’m trying to do a jpa deletion using jsf but I’m not succeeding. have my table.

<p:dataTable value="#{mbProduto.produtos}"
            var="produtos"
            paginator="true"
            rows="10"
            paginatorTemplate="{CurrentPageReport}
            {FirstPageLink}
            {PreviousPageLink} {PageLinks}
            {NextPageLink} {LastPageLink}
            {RowsPerPageDropdown}"
            rowsPerPageTemplate="5,10,15"
            style="width: 80%"
            lazy="true">                
            <p:column>
                    <f:facet name="header">
                        Identificador
                    </f:facet>
                    #{produtos.idProduto}
            </p:column>

            <p:column>
                    <f:facet name="header">
                        Nome
                    </f:facet>
                    #{produtos.nomeProduto}
            </p:column>

            <p:column>
                    <f:facet name="header">
                        Especificações
                    </f:facet>
                    #{produtos.especificacaoProduto}
            </p:column>

            <p:column>
                    <f:facet name="header">
                        Medida Horizontal
                    </f:facet>
                    #{produtos.medidaX}
            </p:column>

            <p:column>
                    <f:facet name="header">
                        Medida Vertical
                    </f:facet>
                    #{produtos.medidaY}
            </p:column>

            <p:column>
                    <f:facet name="header">
                        Preço de Venda
                    </f:facet>
                    #{produtos.precoDeMetroVenda}
            </p:column>

            <p:column>
                    <f:facet name="header">
                        <h:outputText value="Ações" />
                    </f:facet>
                    <h:commandButton value="excluir"
                            action="#{mbProduto.excluir}"
                        id="produtos" ajax="false" onclick="if(!confirm('Deseja excluir #{produtos.nomeProduto}  ?')) return false" />
            </p:column>

            </p:dataTable>

I have my bean

public String excluir() {
    produtoDao.excluir(produto);
    return "pgproduto";
}

she is like requestScoped

and finally got him into my DAO

/*** excluir um produto */
public void excluir(Produto produto) {
    EntityManager em = JPAUtil.getEntityManager();
    em.getTransaction().begin();
    em.remove(produto);
    em.getTransaction().commit();
    em.close();     
}

I thank you and if anyone can give a strength I am grateful

  • What error is getting? This object produto is the selected item, as it is being set in your Bean? Have you considered including the var products in your action (#{mbProdutos.excluir(produtos)})?

  • java.lang.Illegalargumentexception: id to load is required for loading javax.faces.el.Evaluationexception: java.lang.Illegalargumentexception: id to load is required for loading Caused by: java.lang.Illegalargumentexception: id to load is required for loading

2 answers

1

After searching and of course with the help of the staff I got a satisfactory answer the instance of the object needs to be managed and I can do this with the meted merge or find and it is also interesting to pass the selected object to the meteo of exclusion and I did this with the tag<f:setPropertyActionListener value="#{produtos}" target="#{mbProduto.produto}"/> I think that’s all thanks to everyone

0

Error probably occurs because the object produto is in condition detached, that is, outside the context of Entity Manager.

It is important to know that you cannot pass any object to JPA. If that instance is not being managed by it, it is considered a new entity.

The most "secure" in this case is to recover the instance of produto of EntityManager and then request removal.

Example:

public void excluir(Produto produto) {
    EntityManager em = JPAUtil.getEntityManager();
    em.getTransaction().begin();

    //recupera a instância do EntityManager
    produto = em.find(Produto.class, produto.getId());

    em.remove(produto);
    em.getTransaction().commit();
    em.close();     
}
  • did as you said but the error is still pasting some outputs of stactrace java.lang.Illegalargumentexception: id to load is required for loading javax.faces.el.Evaluationexception: java.lang.Illegalargumentexception: id to load is required for loading Caused by: java.lang.Illegalargumentexception: id to load is required for loading

  • @Andremartins In this case the error is deeper. It seems that your object does not have the ID filled. Verify that this is true by debugging (debug) the code and, if confirmed, review the flow of information to ensure that the required data is being filled in. Otherwise, add more information (like the full stack) by editing your question, as the comment space is limited.

  • If you want to take a look at the application https://github.com/AndreMart/comunicaVisual

  • @Andremartins I don’t have time to download the project, configure the whole environment and debug it. If you can’t do this for yourself, maybe later I can get a general look at the project code, but I can’t guarantee it because I’m really busy this week.

Browser other questions tagged

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