2013-12-12 45 views
0

我有關於將新記錄插入數據庫的代碼有問題。這是我的代碼。希望有人建議我。無法使用jsf和hibernate插入數據庫。對象返回null

客戶

public class Customer implements java.io.Serializable { 
    private int cusId; 
    private String firstName; 
    private String lastName; 
    private String email; 
    private Serializable dob; 

    public Customer() { 
    } 

    public Customer(int cusId, String firstName, String lastName, String email, Serializable dob) { 
     this.cusId = cusId; 
     this.firstName = firstName; 
     this.lastName = lastName; 
     this.email = email; 
     this.dob = dob; 
    } 

    public int getCusId() { 
     return this.cusId; 
    } 

    public void setCusId(int cusId) { 
     this.cusId = cusId; 
    } 
    public String getFirstName() { 
     return this.firstName; 
    } 

    public void setFirstName(String firstName) { 
     this.firstName = firstName; 
    } 
    public String getLastName() { 
     return this.lastName; 
    } 

    public void setLastName(String lastName) { 
     this.lastName = lastName; 
    } 
    public String getEmail() { 
     return this.email; 
    } 

    public void setEmail(String email) { 
     this.email = email; 
    } 
    public Serializable getDob() { 
     return this.dob; 
    } 

    public void setDob(Serializable dob) { 
     this.dob = dob; 
    } 
} 

customerBean

@ManagedBean(name="customerBean") 
@RequestScoped 
public class customerBean implements Serializable { 

    private Customer selectedCustomer; 
    private List<Customer> customer; 

    /** Creates a new instance of customerBean */ 
    public customerBean() { 
     selectedCustomer = new Customer(); 
     customer = new ArrayList<Customer>(); 
    } 

    public List<Customer> getCustomer() { 
     CustomersDao cust_dao = new CustomersDao(); 
     customer = cust_dao.findAll(); 
     return customer; 
    } 

    public Customer getSelectedCustomer() { 
     return selectedCustomer; 
    } 

    public void setSelectedCustomer(Customer selectedCustomer) { 
     this.selectedCustomer = selectedCustomer; 
    } 

    public void btnCreate(){ 
     CustomersDao cust_create = new CustomersDao(); 
     String msg; 
     if(cust_create.createCustomer(selectedCustomer)){ 
      msg = "Created Successfully!"; 
     }else{ 
      msg = "Error. Please check again!"; 
     } 
     FacesMessage massage = new FacesMessage(FacesMessage.SEVERITY_INFO, msg, null); 
     FacesContext.getCurrentInstance().addMessage(null, massage); 
    } 
} 

CustomersDao

public class CustomersDao { 
    public boolean createCustomer(Customer customer){ 
     boolean flag; 
     Session session = HibernateUtil.getSessionFactory().getCurrentSession(); 
     try{ 
      session.beginTransaction(); 
      session.save(customer); 
      session.beginTransaction().commit(); 
      flag = true; 
     }catch(Exception e){ 
      flag = false; 
      session.beginTransaction().rollback(); 
     } 
     return flag; 
    } 
} 

XHTML

<p:growl id="msgs" showDetail="true" /> 
<h:form> 
    <p:commandButton id="btnCreate" oncomplete="createDialog.show()" icon="ui-icon-search" title="Create" value="New Customer" update=":formCreate" /> 
</h:form> 
<h:form id="formCreate"> 
    <p:dialog header="Create New Customer" widgetVar="createDialog" resizable="false" id="createDlg" showEffect="fade" hideEffect="explode"> 

     <h:panelGrid id="display" columns="2" cellpadding="4" style="margin:0 auto;"> 

      <h:outputText value="First Name: " /> 
      <h:inputText value="#{customerBean.selectedCustomer.firstName}"/> 

      <h:outputText value="Last Name: " /> 
      <h:inputText value="#{customerBean.selectedCustomer.lastName}" /> 

      <h:outputText value="DOB: " /> 
      <h:inputText value="#{customerBean.selectedCustomer.dob}" /> 

      <h:outputText value="Email: " /> 
      <h:inputText value="#{customerBean.selectedCustomer.email}" /> 

      <f:facet name="footer"> 
       <p:separator /> 
       <p:commandButton id="btnOK" oncomplete="createDialog.hide()" immediate="true" action="#{customerBean.btnCreate}" icon="ui-icon-search" title="Save" value="Save" update=":formData, :msgs" /> 
       <p:commandButton id="btnCancel" oncomplete="createDialog.hide()" icon="ui-icon-search" title="Cancel" value="Cancel" /> 
      </f:facet> 
     </h:panelGrid> 

    </p:dialog> 
</h:form> 

當我跑我在調試模式下的代碼,這是我進入,例如名字,lasst名字,出生日期,電子郵件值回。我試圖改變SessionScope,但沒有改變。我正在使用PrimesFace 3.5,Glassfish 3.1,Hibernate和JSF 2.0

+0

我想你想在CANCEL按鈕上放上那個immediate =「true」,而不是OK按鈕。 – Gimby

+0

@Gimby現在我可以收到我輸入的值,但custId仍然是0,custId是身份字段,爲什麼會發生這種情況? –

+0

因爲您在客戶實體的數據庫或休眠配置中存在錯誤。 – Gimby

回答

1

首先,immediate屬性在保存按鈕中被濫用。只需將其刪除即可讓JSF生命週期以一般方式發生。

其次,您的取消按鈕不會調用任何服務器端操作,因此請通過切換到<p:button>或在<p:commandButton>上定義type="button"來讓它成爲普通按鈕。

最後,你開始新交易三次。將的第二個和第三個調用移除到session.getTransaction(),否則您會顛覆舊的事務(而不是提交它)。

+0

感謝您的支持。我刪除了即時屬性,我可以收到輸入的值。但是當我用調試模式檢查時,cusId仍然是0,在MySQL中它是標識列,我有一個標識爲2的記錄,如果我插入到數據庫中,它應該是3。爲什麼會發生? –

+0

請務必閱讀瞬態,分離和持久實體狀態之間的差異。你基本上是試圖合併分離的實體,從而使其持久化,並且你似乎想根據你的評論來堅持一個短暫的實體。 – skuntsel

+0

您選擇的客戶實例前綴是ambiguos。試着用清晰的詞語表達你想要達到的目標。看起來你正在粘貼代碼的一部分,所以完整的上下文不清楚。除此之外,您對JSF原則的使用是錯誤的,並且您不遵循Java命名約定。 – skuntsel