user2044570
user2044570

Reputation: 31

primefaces row deleting from database

I'm trying to delete row in primefaces datatable from database. It's working fine in datatable, but after refreshing project the row value show up.I'm using http://www.mkyong.com/jsf2/how-to-delete-row-in-jsf-datatable/ example. Can anybody help?

index.xhtml

    <p:growl id="messages" showDetail="true"/>  

    <p:dataTable var="u" value="#{logonTest.userList}" id="carList" editable="true">  

        <f:facet name="header">  
            In-Cell Editing  
        </f:facet>  

        <p:ajax event="rowEdit" listener="#{tableBean.onEdit}" update=":form:messages" />  
        <p:ajax event="rowEditCancel" listener="#{tableBean.onCancel}" update=":form:messages" />

        <p:column headerText="Name" style="width:30%"> 

            <p:cellEditor>  
                <f:facet name="output">  
                    <h:outputText value="#{u.name}" />  
                </f:facet>  
                <f:facet name="input">  
                    <p:inputText value="#{u.name}" style="width:100%"/>  
                </f:facet>  

            </p:cellEditor>  

        </p:column>  

        <p:column headerText="Surname" style="width:20%">  
            <p:cellEditor>  
                <f:facet name="output">  
                    <h:outputText value="#{u.surname}" />  
                </f:facet>  
                <f:facet name="input">  
                    <p:inputText value="#{u.surname}" style="width:100%" />  
                </f:facet>  
            </p:cellEditor>  
        </p:column>  

        <p:column headerText="Username" style="width:24%">  
           <p:cellEditor>  
                <f:facet name="output">  
                    <h:outputText value="#{u.username}" />  
                </f:facet>  
                <f:facet name="input">  
                    <p:inputText value="#{u.username}" style="width:100%" />  
                </f:facet>  
            </p:cellEditor>  
        </p:column>  

        <p:column headerText="Description" style="width:20%">  
           <p:cellEditor>  
                <f:facet name="output">  
                    <h:outputText value="#{u.description}" />  
                </f:facet>  
                <f:facet name="input">  
                    <p:inputText value="#{u.description}" style="width:100%" />  
                </f:facet>  
            </p:cellEditor>  
        </p:column>  

        <p:column style="width:6%">  
            <p:rowEditor />  
    <h:commandLink value="Delete" action="#{logonTest.deleteAction(u)}" />
        </p:column>  

    </p:dataTable>  

</h:form>  

LogonTest.java

@ViewScoped
@SessionScoped
@javax.faces.bean.ManagedBean(name = "logonTest")
public class LogonTest implements Serializable{
     @PersistenceUnit(unitName="Webbeans_RESOURCE_LOCAL")
     private EntityManagerFactory emf;
    public List<User> getUserList() {
        return userList;
    }

    public void setUserList(List<User> userList) {
        this.userList = userList;
    }

    public List<User> userList = new ArrayList();


    @PostConstruct
    public void init(){
         EntityManager em = emf.createEntityManager();
         // Read the existing entries and write to console
         Query q = em.createQuery("SELECT u FROM User u");
         userList = q.getResultList();
         System.out.println("Size: " + userList.size());
    }

    public LogonTest() {

    }
    public String deleteAction(User user) {

        userList.remove(user);
        return null;
    }
}

Upvotes: 0

Views: 1339

Answers (3)

Tiago Vieira Dos Santos
Tiago Vieira Dos Santos

Reputation: 1004

Try the following code with EntityManager using remove method.

@ViewScoped
@SessionScoped
@javax.faces.bean.ManagedBean(name = "logonTest")
public class LogonTest implements Serializable{
     @PersistenceUnit(unitName="Webbeans_RESOURCE_LOCAL")
     private EntityManagerFactory emf;
    public List<User> getUserList() {
        return userList;
    }

    public void setUserList(List<User> userList) {
        this.userList = userList;
    }

    public List<User> userList = new ArrayList();


    @PostConstruct
    public void init(){
         EntityManager em = emf.createEntityManager();
         // Read the existing entries and write to console
         Query q = em.createQuery("SELECT u FROM User u");
         userList = q.getResultList();
         System.out.println("Size: " + userList.size());
    }

    public LogonTest() {

    }
    public String deleteAction(User user) {
        EntityManager em = emf.createEntityManager();
        em.remove(user);
        userList.remove(user);
        return null;
    }
}

Upvotes: 0

VeenarM
VeenarM

Reputation: 1263

Your delete action is only removing it from the scoped variable.

public String deleteAction(User user) {
    userList.remove(user);
    return null;
}

You need to also remove it from the database, via the entity manager. em.remove(object)

Upvotes: 0

Omar NourdeaN
Omar NourdeaN

Reputation: 503

that because you remove it from the arraylist only not from the database
use em.remove(user)

Upvotes: 2

Related Questions