Search code examples
jsfprimefacesmanaged-bean

Primefaces datatable with delete button not working


I'm trying to delete one cell with a <p:commandButton> and after i click i want to update my table. But all what happens is, that i got this Exception.

 javax.persistence.EntityNotFoundException: Unable to find de.test.Datei with id 5

But in my Database i can see the facts in each table. Also when i click the button, the dates will be delete correctly. But my table doesn't update, so the cell isn't disappeared. And when i click twice, i got the above error message.

Datei.class

@Entity
public class Datei implements Serializable {


private static final long serialVersionUID = 1L;

@Id
@SequenceGenerator(name = "DATA_ID_GENERATOR", sequenceName = "SEQ_DATA", allocationSize = 1, initialValue = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "DATA_ID_GENERATOR")
private long id;
private String name;
private long groesse;
@Temporal(TemporalType.TIMESTAMP)
private Date datum;
@Basic(fetch = FetchType.LAZY)
@Lob
private byte[] datei;
@ManyToOne
private Benutzer benutzer;

public Benutzer getBenutzer() {
    return benutzer;
}

public void setBenutzer(Benutzer benutzer) {
    this.benutzer = benutzer;
}

public long getId() {
    return id;
}

public void setId(long id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public long getGroesse() {
    return groesse;
}

public void setGroesse(long groesse) {
    this.groesse = groesse;
}

public byte[] getDatei() {
    return datei;
}

public void setDatei(byte[] datei) {
    this.datei = datei;
}

public Datei() {
    // TODO Auto-generated constructor stub
}

public Date getDatum() {
    return datum;
}

public void setDatum(Date datum) {
    this.datum = datum;
}


@Override
public String toString() {
    return "Datei [id=" + id + ", name=" + name + ", groesse=" + groesse
            + ", datum=" + datum + ", datei=" + Arrays.toString(datei)
            + "]";
}

}

Benutzer.class

@Entity
public class Benutzer implements Serializable {

private static final long serialVersionUID = 1L;

@Id
@SequenceGenerator(name = "CUSTOMER_ID_GENERATOR", sequenceName = "SEQ_CUSTOMER", allocationSize = 1, initialValue = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "CUSTOMER_ID_GENERATOR")
private long id;
private String name;
private String email;
private long kundennummer;
@OneToMany(mappedBy = "benutzer", orphanRemoval = true)
private List<Datei> datei;

@Override
public String toString() {
    return "Benutzer [id=" + id + ", name=" + name + ", email=" + email
            + ", kundennummer=" + kundennummer + ", datei=" + datei + "]";
}

public List<Datei> getDatei() {
    return datei;
}

public void setDatei(List<Datei> datei) {
    this.datei = datei;
}

public long getId() {
    return id;
}

public void setId(long id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public long getKundennummer() {
    return kundennummer;
}

public void setKundennummer(long kundennummer) {
    this.kundennummer = kundennummer;
}

public Benutzer() {
    // TODO Auto-generated constructor stub
}

}  

My DateiDAO.class

[...]
 public void loeschen(Datei datei, Benutzer benutzer) {

    EntityManager em = emf.createEntityManager();
    EntityTransaction tr = em.getTransaction();

    tr.begin();
    benutzer = em.merge(benutzer);
    datei = em.merge(datei);
    tr.commit();

    benutzer.getDatei().remove(datei);

    tr.begin();
    em.merge(benutzer);
    em.remove(em.merge(datei));
    tr.commit();
}

So the only problem is, that my table dosen't update. If i use richfaces, it works fine.

main.xhtml

<h:form id="myForm">
        <p:dataTable id="dateien" var="data"
                value="#{mainController.ben.datei}" rowKey="#{data.id}"
                selectionMode="single" rows="5" paginator="true" rowsPerPageTemplate="5,10,15" paginatorPosition="bottom"
                paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}">
                <f:facet name="header">
                    <h:outputText value="Uploaded"/>
                </f:facet>
                <p:column headerText="Id">
                    <h:outputText value="#{data.id}" />
                </p:column>
                <p:column headerText="Name">
                    <h:outputText value="#{data.name}" />
                </p:column>
                <p:column headerText="Datum">
                    <h:outputText value="#{data.datum}" />
                </p:column>
                <p:column headerText="Delete">
                    <p:commandButton update="myForm"
                        actionListener="#{mainController.loeschen(data)}"
                        icon="ui-icon-trash" />
                </p:column>
            </p:dataTable>
        </h:form>

Solution

  • Add update="dateien" to your commandButton

    <p:commandButton update="myForm"
                     actionListener="#{mainController.loeschen(data)}"
                     icon="ui-icon-trash" 
                     update="dateien"/>