Hibernate saves twice the entity executed within the primefaces dialog

Asked

Viewed 125 times

1

When the save button is on the page it works normally and saves the entity but I open a dialog to confirm the data to be saved and Hibernate saves twice very strange method to save outside the dialog works properly but inside runs twice.

<p:dialog id="dialogconfirmarvenda" appendTo="@(body)" widgetVar="dcv" closable="true" width="1000" maximizable="true" binding="#{vendaMB.dialogconfirmarvenda}" minimizable="true" position="top">
    <p:ajax event="close" listener="#{vendaMB.closeDialog()}" />
    <f:facet name="header"><h:outputText value="CONFIRMAR VENDA" style="font-family: sans-serif"/></f:facet>
    <h:form id="formfinalizarvenda">
        <p:panelGrid id="panelgridconfirmarvenda">
            <p:row>
                <p:column rendered="#{vendaMB.clienteMB.clienteDAO.cliente != null}"><h:outputText value="Cliente" styleClass="text"/></p:column>
                <p:column rendered="#{vendaMB.clienteMB.clienteDAO.cliente != null}"><h:outputText value="#{vendaMB.clienteMB.clienteDAO.cliente.nome}" styleClass="text"/></p:column>
                <p:column><h:outputText value="Pagamento" styleClass="text"/></p:column>
                <p:column><h:outputText value="#{vendaMB.meioPgtoMB.meioPgtoDAO.meiopgto.descricao}" styleClass="text"/></p:column>
                <p:column><h:outputText value="Total da Venda" styleClass="text"/></p:column>
                <p:column><h:outputText value="#{vendaMB.totalVenda}" styleClass="text"><f:convertNumber type="currency"/></h:outputText></p:column>
                <p:column>
                    <p:commandButton actionListener="#{vendaMB.finalizarVenda()}" value="Finalizar" oncomplete="PF('dcv').hide()"/>
                </p:column>
            </p:row>
        </p:panelGrid>
        <p:spacer height="10" width="800"/>
        <p:dataTable id="tableitensvenda" value="#{vendaMB.itemvendaAdds}" var="ivadd">
            <p:column headerText="Código"><h:outputText value="#{ivadd.itemvenda.produto.codigo}"/></p:column>
            <p:column headerText="Produto"><h:outputText value="#{ivadd.itemvenda.produto.descricao}"/></p:column>
            <p:column headerText="Preço">
                <h:outputText value="#{ivadd.itemvenda.valor}">
                    <f:convertNumber type="currency"/>
                </h:outputText>
            </p:column>
            <p:column headerText="Qtd"><h:outputText value="#{ivadd.itemvenda.quantidade}"/></p:column>
            <p:column headerText="Total"><h:outputText value="#{ivadd.total}"><f:convertNumber type="currency"/></h:outputText></p:column>
        </p:dataTable>
    </h:form>
</p:dialog>

This is the dialog to display the data for confirmation. The link that opens the dialog:

<p:commandLink value="venda" title="Confirmar Venda" oncomplete="PF('dcv').show()" actionListener="#{vendaMB.confirmarVenda}" />

public void confirmarVenda() {
    for (Meiopgto meiopgto : meioPgtoMB.getMeiopgtos()) {
        if (meiopgto.getIdmeiopgto().equals(idmeiopgto)) {
            meioPgtoMB.getMeioPgtoDAO().setMeiopgto(meiopgto);
            break;
        }
    }
    RequestContext.getCurrentInstance().update(Arrays.asList("formvenda", "dialogconfirmarvenda"));
}
  • Ronaldo, welcome to Stack Overflow. In order for us to assist you properly it is important that you provide the relevant information from a technical point of view. This includes code grafts, Markup, settings and everything else you need to reproduce your problem. Read on How to create a Minimum, Complete and Verifiable example and take the time you need to update the question.

  • Ronaldo, could include the code of the button that opens the Dialog? My guess is that the button has a action calling the method vendaMB.finalizarVenda(), and in addition to opening the Dialog he saves the information.

  • Do not forget to make a mention (@Name), otherwise I am not notified. My suggestion would be to put a Breakpoint in the method that is called twice and look at the stacktrace to see what it is called in those two times. This may give a hint of the problem.

  • Ronaldo, I wanted you to put one Breakpoint and debug in the method that calls to JPA. And if possible include the stacktrace that appears in the Debug perspective.

  • Some possibilities (the problem of not having one MVCE is not being able to state anything accurately). Is your dialog not inside a form and this is firing the event twice? Did some other listener as #{vendaMB.closeDialog()} or #{vendaMB.finalizarVenda()} is not calling this method?

  • @Anthony Accioly I removed the Binding="#{saleMB.dialogconfirma sale}" and it worked - I don’t know why - and thanks for the help.

  • 1

    @Ronaldosantos, as you may have seen in the [tour], you can accept a response, do not use the "Solved" approach in the title as is usually done in forums. In this question, you could elaborate an answer telling how you arrived at the solution and then accept it.

Show 2 more comments

1 answer

1

I solved the problem by removing the Binding="#{saleMB.dialogconfirma sale}" dialog

Browser other questions tagged

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