我对开发Java EE和JSF Web应用程序非常陌生。单击按钮,我无法从数据库中删除对象。
所以我正在做的是点击我的JSF页面上的按钮
<h:commandButton value="Remove" action="#{productManagementBean.removeProd(product.id)}"/>
这将在我的ProductManagementBean中调用名为removeProd(Integer);
@Named(value = "productManagementBean")
@RequestScoped
public class ProductManagementBean implements Serializable {
@EJB
private OperationsBeanLocal operationsBean;
@Inject
CurrentUserBean currentUser;
public ProductManagementBean() {
}
public String getQuery() {
String Query = FacesContext.getCurrentInstance().getExternalContext()
.getRequestParameterMap().get("query");
return Query;
}
public void checkIfQueryExists() throws IOException {
if (operationsBean.checkIfQueryExists(getQuery()) == 0) {
FacesContext.getCurrentInstance().getExternalContext().redirect("error.xhtml");
}
}
public List<Products> getProducts() {
return operationsBean.getAllProducts();
}
public Products getProduct() {
return operationsBean.getProduct(getQuery());
}
public Products getProductById(String id) {
return operationsBean.getProductById(Integer.parseInt(id));
}
public String getProductByName(String name) {
String returnString = "";
Products p = operationsBean.getProductByName(name);
if (p != null) {
String temp = p.getName();
temp = temp.replaceAll(" ", "+");
returnString = "product.xhtml?query=" + temp;
} else {
returnString = "error";
}
return returnString;
}
public void removeProd(Integer id) {
operationsBean.removeProd(id);
}
}
该方法将在我的operationsBean中调用一个方法
@Stateless
public class OperationsBean implements OperationsBeanLocal {
@PersistenceContext(unitName = "My_Project-ejbPU")
private EntityManager em;
@Override
public List<Products> getAllProducts() {
return em.createQuery("SELECT p FROM Products p").getResultList();
}
@Override
public int checkIfQueryExists(String query) {
List<Products> products = em.createQuery("SELECT p FROM Products p WHERE p.description = :description")
.setParameter("description", query)
.getResultList();
return products.size();
}
@Override
public Products getProduct(String query) {
// Products product = (Products) em.createQuery("SELECT p FROM Products p WHERE p.name = :name")
// .setParameter("name", query)
// .getSingleResult();
Products p = null;
List<Products> products = em.createQuery("SELECT p FROM Products p WHERE p.name = :name")
.setParameter("name", query)
.getResultList();
if (!products.isEmpty()) {
p = products.get(0);
}
return p;
}
@Override
public Products getProductById(Integer id) {
Products product = (Products) em.createQuery("SELECT p FROM Products p WHERE p.id = :id")
.setParameter("id", id)
.getSingleResult();
return product;
}
@Override
public Products getProductByName(String name) {
Products product = (Products) em.createQuery("SELECT p FROM Products p WHERE p.name = :name")
.setParameter("name", name)
.getSingleResult();
return product;
}
@Override
public void removeProd(Integer id) {
List<Products> products = em.createQuery("SELECT p FROM Products p WHERE p.id = :id")
.setParameter("id", id)
.getResultList();
if(!products.isEmpty()) {
delete(products.get(0));
}
}
public void delete(Object object) {
em.getTransaction().begin();
em.remove(object);
em.getTransaction().commit();
}
public void persist(Object object) {
em.persist(object);
}
}
这一切都运行良好,没有例外,但问题是产品从未真正从数据库中删除
答案 0 :(得分:0)
您必须在Products Class中覆盖equals和hashcode方法,否则您可以尝试使用
public void delete(Object object) {
em.getTransaction().begin();
em.remove(em.merge(object));
em.getTransaction().commit();
}