package com.jee6.beans;
import com.jee6.entity.Employee;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Resource;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.persistence.*;
import javax.transaction.SystemException;
@ManagedBean
@SessionScoped
public class EmployeeBean {
@PersistenceContext(unitName = "ServicePU")
private EntityManager em;
@Resource
private javax.transaction.UserTransaction utx;
private List<Employee> emplist=new ArrayList<Employee>();
//private Query sql="SELECT e FROM Employee e";
private Employee emp= new Employee();
public Employee getEmp() {
return emp;
}
public void setEmp(Employee emp) {
this.emp = emp;
}
public List<Employee> getEmplist() {
return emplist=findAll();
}
public void setEmplist(List<Employee> emplist) {
this.emplist = emplist;
}
public List<Employee> findAll(){
emplist=(List<Employee>)em.createQuery("Select e From Employee e").getResultList();
return emplist;
}
public EmployeeBean() {
}
public String saveEmployee(){
persist(emp);
return null;
}
public String deleteEmployee(Employee e){
try{
em.remove(e);
emplist=findAll();
}
catch (Exception ex)
{
Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exceptioncaught", ex);
}
return null;
}
public void persist(Object object) {
try {
utx.begin();
em.persist(object);
utx.commit();
} catch (Exception e) {
Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", e);
throw new RuntimeException(e);
}
}
}
我想从 JSF 中的 DataTable 中删除数据,上面的代码没有输出。我正在尝试修复该代码并且没有输出。在此之前,我在 (em.remove(e);) 的删除方法中遇到了 IllegalException。有没有人可以纠正我并对此表示赞赏。