0

I am doing a mini jsf project that is created by using jpa. I have listed costumers and I can remove them one by one, but if I want to remove selected costumers I cannot do it.

This part is from Costumer class

@Transient
private Boolean selection = false;

And this part is from CostumerBean

Costumer costumer = new Costumer();

private List<Costumer> costumerList = new ArrayList<>();
//(getters and setters)

public void removeSelected() {
    EntityManager em = emf.createEntityManager();

    em.getTransaction().begin();
    for (Costumer cos : costumerList) {
        if (cos.getSelection()) {

em.remove(em.contains(costumer) ? customer : em.merge(costumer));       
        }

    }

    em.getTransaction().commit();

    musteri = new Musteri();
}

And finally here is the xhtml page :

<h:form id="firstForm">

<p:commandButton action="#{customerBean.removeSelection}" value="Remove"
        update="customerForm">
</p:commandButton>
</h:form id="firstForm">


<h:form id="customerForm">

    <p:dataTable value="#{customerBean.customerList}" var="cst"
        id="cstTable">

        <p:column>
            <f:facet name="header">Select</f:facet>
            <p:selectBooleanCheckbox value="#{cst.selection}" />
        </p:column>

        <p:column>
            <f:facet name="header">Name</f:facet>
            #{cst.name}
        </p:column>
</h:form id="customerForm">

I reckon actual problem is I cannot make selection field in Costumer.java true. No action I see.?

berkancetin
  • 186
  • 2
  • 15
  • I have no idea what this line trying to do `em.remove(em.contains(costumer) ? musteri : em.merge(costumer));` – gozluklu_marti May 22 '19 at 11:30
  • it removes the customer object. – berkancetin May 22 '19 at 11:33
  • I know it is for safe remove but you should do something like this: `em.remove(em.contains(cos) ? cos : em.merge(cos));` because you are in a for loop and working with only `cos` object. – gozluklu_marti May 22 '19 at 11:41
  • Maybe, but problem is different. When I debug it, I can not evet come this line. It cannot pass the if block. Becuase my do not change selection. – berkancetin May 22 '19 at 11:55
  • Set the commandButton into the same form as the datatable. The CommandButton is only sending the files in the form by default – Raphael_S May 22 '19 at 12:05
  • Perfect! Thank you @Raphael_S it wroked. Do you know how I can access different form? Is it possible? – berkancetin May 22 '19 at 12:51
  • I did not know what is really about so I added those tags @Kukeltje Thanks for the link – berkancetin May 22 '19 at 13:34
  • @Macanga for submitting multiple forms check this out https://stackoverflow.com/questions/8563299/submit-multiple-forms-with-one-submit-button – Raphael_S May 22 '19 at 13:35

0 Answers0