我有關於將新記錄插入數據庫的代碼有問題。這是我的代碼。希望有人建議我。無法使用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
我想你想在CANCEL按鈕上放上那個immediate =「true」,而不是OK按鈕。 – Gimby
@Gimby現在我可以收到我輸入的值,但custId仍然是0,custId是身份字段,爲什麼會發生這種情況? –
因爲您在客戶實體的數據庫或休眠配置中存在錯誤。 – Gimby