2012-07-11 41 views
1

我想刪除jsf數據表中的一行。我使用jsf和hibernate和spring。但刪除操作不起作用。 Customermanagedbean.java刪除jsf數據表中的行

@ManagedBean(name="CustomerMB") 
@RequestScoped 
public class Customermanagedbean implements Serializable{ 
@ManagedProperty(value="#{CustomerBoImpl}") 
ICustomerBo customerBoImpl; 
List<Customer> CustomerList; 
public int customerId; 
public String name; 
public String address; 
public String createdDate; 



public ICustomerBo getCustomerBoImpl() { 
    return customerBoImpl; 
} 
public void setCustomerBoImpl(ICustomerBo customerBoImpl) { 
    this.customerBoImpl = customerBoImpl; 
} 

public List<Customer> getCustomerList() { 
    CustomerList=new ArrayList<Customer>(); 
    CustomerList.addAll(getCustomerBoImpl().findAllCustomer()); 

    return CustomerList; 
} 



public void setCustomerList(List<Customer> customerList) { 
    CustomerList = customerList; 
} 
public String deleteCustomer(Customer customer){ 
    getCustomerBoImpl().deleteCustomer(customer); 

    return ""; 


}// getter and setter method 

CustomerBoImpl.java

@Transactional(readOnly = true) 
public class CustomerBoImpl implements ICustomerBo{ 

    ICustomerDao customerDaoImpl; 



    public ICustomerDao getCustomerDaoImpl() { 
     return customerDaoImpl; 
    } 

    public void setCustomerDaoImpl(ICustomerDao customerDaoImpl) { 
     this.customerDaoImpl = customerDaoImpl; 
    } 
@Transactional(readOnly = false) 
@Override 
    public void deleteCustomer(Customer customer){ 
     getCustomerDaoImpl().deleteCustomer(customer); 
    } 

@Override 
    public List<Customer> findAllCustomer(){ 

     return getCustomerDaoImpl().findAllCustomer(); 
    } 
} 

CustomerDaoImpl.java

public class CustomerDaoImpl implements ICustomerDao{ 

    private SessionFactory sessionFactory; 
    public SessionFactory getSessionFactory() { 
     return sessionFactory;} 
    public void setSessionFactory(SessionFactory sessionFactory) { 
     this.sessionFactory = sessionFactory; 
    } 
public void deleteCustomer(Customer customer){ 
     sessionFactory.openSession(); 
     getSessionFactory().getCurrentSession().delete(customer); 
    } 


    public List<Customer> findAllCustomer(){ 
     sessionFactory.openSession(); 

     List list = getSessionFactory().getCurrentSession 

().createQuery("from Customer").list(); 
     return list; 

    } 
} 

defualt.xhtml

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" 
     xmlns:h="http://java.sun.com/jsf/html" 
     xmlns:f="http://java.sun.com/jsf/core" 
     > 
    <h:head> 
     <h:outputStylesheet library="css" name="table-style.css" /> 
    </h:head> 

    <h:body> 



     <h:dataTable value="#{CustomerMB.getCustomerList()}" var="c" 
       styleClass="order-table" 
       headerClass="order-table-header" 
       rowClasses="order-table-odd-row,order-table-even-row" 
      > 

      <h:column> 
       <f:facet name="header"> 
        Customer ID 
       </f:facet> 
        #{c.customerId} 
      </h:column> 

      <h:column> 
       <f:facet name="header"> 
        Name 
       </f:facet> 
        #{c.name} 
      </h:column> 

      <h:column> 
       <f:facet name="header"> 
        Address 
       </f:facet> 
        #{c.address} 
      </h:column> 

      <h:column> 
       <f:facet name="header"> 
        Created Date 
       </f:facet> 
        #{c.createdDate} 

      </h:column> 
      <h:column> 

    <f:facet name="header">Action</f:facet> 

    <h:commandButton value="Delete" action="#{CustomerMB.deleteCustomer(c)}" /> 

      </h:column> 

     </h:dataTable> 

有什麼不好?請幫幫我。

+0

您是否得到任何異常? – Ravi 2012-07-12 00:21:13

+0

你可以嘗試通過列表''而不是方法簽名 – Mango 2012-07-12 09:01:12

回答

0

檢查您的CustomerDaoImpl類的deleteCustomer方法。你可以嘗試像下面示例

public void deleteCustomer(Customer customer){ 
     getHibernateTemplate().delete(customer); 
    } 

在你CustomerBoImpl類,改變這個

@Autowired 
    ICustomerDao customerDaoImpl; 

解決方法是創建一個自定義類(CustomHibernateDaoSupport)和擴展「的HibernateDaoSupport」和自動線的會議工廠和您的DAO類擴展此類

 public abstract class CustomHibernateDaoSupport extends HibernateDaoSupport{  
      @Autowired 
      public void anyMethodName(SessionFactory sessionFactory){ 
       setSessionFactory(sessionFactory); 
      } 
     } 
+0

我使用的是hibernate 4.1.4,它不支持HibernateDaoSupport – samira 2012-07-14 07:24:16