I'm trying to make delete row function in primefaces datatable. I'm using this method:
package logon;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.enterprise.context.SessionScoped;
import javax.faces.bean.ViewScoped;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;
import javax.persistence.Query;
@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(em.getReference(User.class, user.getId()));
userList.remove(user);
return null;
}
}
index.xhtml
<h:commandLink value="Delete" action="#{logonTest.deleteAction(u)}" />
Row is deleted in primefaces datatable, but not in database. After restarting project deleted row is showing up. Can someone help?