以下代码遵循[本教程](http://javaknowledge.info/simple-crud-using-jsf2hibernate-integration-and-mysql/)并带来一些其他问题。
以下是我的模特:
Customer.java
package com.javaknowledge.entity;
import com.javaknowledge.dao.CustomerDao;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import org.primefaces.model.UploadedFile;
import java.sql.Blob;
/**
*
* @author VENNY
*/
@ManagedBean
@SessionScoped
public class Customer implements java.io.Serializable {
private String VIDPROSPEK;
private String VNAME;
private String VADRESS;
private String NPHONENUM;
private String KILOMETER;
private String TAHUNPRODUKSI;
private String sd, msg, selectedname;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
public Customer() {
}
public Customer(String VIDPROSPEK, String VNAME, String VADRESS, String NPHONENUM, String TAHUNPRODUKSI, String KILOMETER) {
this.VIDPROSPEK = VIDPROSPEK;
this.VNAME = VNAME;
this.VADRESS = VADRESS;
this.NPHONENUM = NPHONENUM;
this.TAHUNPRODUKSI= TAHUNPRODUKSI;
this.KILOMETER = KILOMETER;
}
public String getSd() {
return sd;
}
public void setSd(String sd) {
this.sd = sd;
}
public String getVIDPROSPEK() {
return this.VIDPROSPEK;
}
public void setVIDPROSPEK(String VIDPROSPEK) {
this.VIDPROSPEK = VIDPROSPEK;
}
public String getVNAME() {
return this.VNAME;
}
public void setVNAME(String VNAME) {
this.VNAME = VNAME;
}
public String getVADRESS() {
return this.VADRESS;
}
public void setVADRESS(String VADRESS) {
this.VADRESS = VADRESS;
}
public String getNPHONENUM() {
return this.NPHONENUM;
}
public void setNPHONENUM(String NPHONENUM) {
this.NPHONENUM = NPHONENUM;
}
public String getKILOMETER() {
return this.KILOMETER;
}
public void setKILOMETER(String KILOMETER) {
this.KILOMETER = KILOMETER;
}
public String getTAHUNPRODUKSI() {
return this.TAHUNPRODUKSI;
}
public void setTAHUNPRODUKSI(String TAHUNPRODUKSI) {
this.TAHUNPRODUKSI = TAHUNPRODUKSI;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public String getSelectedname() {
return selectedname;
}
public void setSelectedname(String selectedname) {
this.selectedname = selectedname;
}
public void saveCustomer() {
CustomerDao dao = new CustomerDao();
dao.addCustomer(this);
this.msg = "Member Info Saved Successfull!";
clearAll();
}
public void updateCustomer() {
CustomerDao dao = new CustomerDao();
dao.updateCustomer(this);
this.msg = "Member Info Update Successfull!";
clearAll();
}
public void deleteCustomer() {
CustomerDao dao = new CustomerDao();
dao.deleteCustomer(VIDPROSPEK);
this.msg = "Member Info Delete Successfull!";
clearAll();
}
public List<Customer> getAllCustomers() {
List<Customer> users = new ArrayList<Customer>();
CustomerDao dao = new CustomerDao();
users = dao.getAllCustomers();
return users;
}
public void fullInfo() {
CustomerDao dao = new CustomerDao();
List<Customer> lc = dao.getCustomerById(selectedname);
System.out.println(lc.get(0).VNAME);
this.VIDPROSPEK = lc.get(0).VIDPROSPEK;
this.VNAME = lc.get(0).VNAME;
this.VADRESS = lc.get(0).VADRESS;
this.NPHONENUM = lc.get(0).NPHONENUM;
this.TAHUNPRODUKSI = lc.get(0).TAHUNPRODUKSI;
//this.sd = sdf.format(dob);VIMAGE
}
private void clearAll() {
this.VNAME = "";
this.VADRESS = "";
this.NPHONENUM="";
this.KILOMETER="";
}
}
TipeMotor.java
package com.javaknowledge.entity; import com.javaknowledge.dao.TipeMotorDAO; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import javax.faces.bean.ManagedBean; import javax.faces.bean.SessionScoped;/** * * @author VENNY */ @ManagedBean @SessionScoped public class TipeMotor implements java.io.Serializable {
private String VTYPECODE;
private String VCLRCODE;
private String VCLRDESC;
private String selectedVCLRDESC;
public TipeMotor() {
}
public TipeMotor(String VTYPECODE, String VCLRCODE, String VCLRDESC) {
this.VTYPECODE = VTYPECODE;
this.VCLRCODE = VCLRCODE;
this.VCLRDESC = VCLRDESC;
}
public String getVTYPECODE() {
return VTYPECODE;
}
public void setVTYPECODE(String VTYPECODE) {
this.VTYPECODE = VTYPECODE;
}
public String getVCLRCODE() {
return VCLRCODE;
}
public void setVCLRCODE(String VCLRCODE) {
this.VCLRCODE = VCLRCODE;
}
public String getVCLRDESC() {
return VCLRDESC;
}
public void setVCLRDESC(String VCLRDESC) {
this.VCLRDESC = VCLRDESC;
}
public String getselectedVCLRDESC() {
return selectedVCLRDESC;
}
public void setselectedVCLRDESC(String selectedVCLRDESC) {
this.selectedVCLRDESC = selectedVCLRDESC;
}
public List<TipeMotor> getAllTipeMotors() {
List<TipeMotor> users = new ArrayList<TipeMotor>();
TipeMotorDAO dao = new TipeMotorDAO();
users = dao.getAllTipeMotors();
return users;
} }
hibernate.reveng.xml中
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-reverse-engineering PUBLIC "-//Hibernate/Hibernate Reverse Engineering DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-reverse-engineering-3.0.dtd">
<hibernate-reverse-engineering>
<schema-selection match-catalog="ahmids"/>
<table-filter match-name="ahmdsmun_mstprospek"/>
<table-filter match-name="ahmdsmun_msttypeclr"/>
</hibernate-reverse-engineering>
Customer.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Jul 5, 2013 10:57:32 AM by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
<class name="com.javaknowledge.entity.Customer" table="ahmdsmun_mstprospek" catalog="ahmids">
<id name="VIDPROSPEK" type="string">
<column name="VIDPROSPEK" />
</id>
<property name="VNAME" type="string">
<column name="VNAME" length="100" not-null="true" />
</property>
<property name="VADRESS" type="string">
<column name="VADRESS" length="100" not-null="true" />
</property>
<property name="NPHONENUM" type="string">
<column name="NPHONENUM" length="15" />
</property>
<property name="TAHUNPRODUKSI" type="string">
<column name="TAHUNPRODUKSI" length="4"/>
</property>
<property name="KILOMETER" type="string">
<column name="KILOMETER" length="100" />
</property>
</class>
</hibernate-mapping>
和TipeMotor.hbm.xml中的类似内容
TipeMotorDAO.java
package com.javaknowledge.dao;
import com.javaknowledge.entity.TipeMotor;
import com.javaknowledge.util.HibernateUtil;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
/**
*
* @author javaknowledge
*/
public class TipeMotorDAO {
public void addTipeMotor(TipeMotor timot) {
Transaction trns = null;
Session session = HibernateUtil.getSessionFactory().openSession();
try {
trns = session.beginTransaction();
session.save(timot);
session.getTransaction().commit();
} catch (RuntimeException e) {
if (trns != null) {
trns.rollback();
}
e.printStackTrace();
} finally {
session.flush();
session.close();
}
}
public void deleteCustomer(int custid) {
Transaction trns = null;
Session session = HibernateUtil.getSessionFactory().openSession();
try {
trns = session.beginTransaction();
TipeMotor cust = (TipeMotor) session.load(TipeMotor.class, new Integer(custid));
session.delete(cust);
session.getTransaction().commit();
} catch (RuntimeException e) {
if (trns != null) {
trns.rollback();
}
e.printStackTrace();
} finally {
session.flush();
session.close();
}
}
public void updateTipeMotor(TipeMotor cust) {
Transaction trns = null;
Session session = HibernateUtil.getSessionFactory().openSession();
try {
trns = session.beginTransaction();
session.update(cust);
session.getTransaction().commit();
} catch (RuntimeException e) {
if (trns != null) {
trns.rollback();
}
e.printStackTrace();
} finally {
session.flush();
session.close();
}
}
public List<TipeMotor> getAllTipeMotors() {
List<TipeMotor> users = new ArrayList<TipeMotor>();
Transaction trns = null;
Session session = HibernateUtil.getSessionFactory().openSession();
try {
trns = session.beginTransaction();
users = session.createQuery("select VCLRDESC from ahmdsmun_mstprospek").list();
} catch (RuntimeException e) {
e.printStackTrace();
} finally {
session.flush();
session.close();
}
return users;
}
}
类似于CustomerDAO.java
中的内容和类HibernateUtil.jva
最后,这是我的索引
<?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">
<f:view xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui"
xmlns:pm="http://primefaces.org/mobile"
contentType="text/html">
<h:head>
<title>PKB Mobile</title>
</h:head>
<h:body>
<pm:page id="customer" title="Hello Primefaces Mobile">
<pm:header title="PKB MOBILE" >
<p:commandButton styleClass="ui-btn-right ui-btn-inline" value="Summary" icon="ui-icon-gear" action="#{customer.saveCustomer}" />
<p:button styleClass="ui-btn-left ui-btn-inline" value="Reset" icon="ui-icon-gear" />
</pm:header>
<pm:content>
<h:form>
<p:tabMenu>
<p:menuitem value="Customer" />
<p:menuitem value="Service" />
<p:menuitem value="Part"/>
</p:tabMenu>
<table>
<tr>
<td><h:outputLabel value="VIDPROSPEK:"/></td>
<td><h:inputText value="#{customer.VIDPROSPEK}" id="VIDPROSPEK"/></td>
</tr>
<tr>
<td width="200">
<p:outputLabel for="TAHUNPRODUKSI" value="Tahun Produksi" />
</td>
<td width="500">
<h:selectOneMenu id="TAHUNPRODUKSI" value="#{customer.TAHUNPRODUKSI}">
<f:selectItem itemLabel="Select One" itemValue="" />
<f:selectItem itemLabel="1991" itemValue="1991" />
<f:selectItem itemLabel="1992" itemValue="1992" />
<f:selectItem itemLabel="1993" itemValue="1993" />
<f:selectItem itemLabel="1994" itemValue="1994" />
<f:selectItem itemLabel="1995" itemValue="1995" />
<f:selectItem itemLabel="1996" itemValue="1996" />
<f:selectItem itemLabel="1997" itemValue="1997" />
<f:selectItem itemLabel="1998" itemValue="1998" />
<f:selectItem itemLabel="1999" itemValue="1999" />
<f:selectItem itemLabel="2000" itemValue="2000" />
<f:selectItem itemLabel="2001" itemValue="2001" />
<f:selectItem itemLabel="2002" itemValue="2002" />
<f:selectItem itemLabel="2003" itemValue="2003" />
<f:selectItem itemLabel="2004" itemValue="2004" />
<f:selectItem itemLabel="2005" itemValue="2005" />
<f:selectItem itemLabel="2006" itemValue="2006" />
<f:selectItem itemLabel="2007" itemValue="2007" />
<f:selectItem itemLabel="2008" itemValue="2008" />
<f:selectItem itemLabel="2009" itemValue="2009" />
<f:selectItem itemLabel="2010" itemValue="2010" />
<f:selectItem itemLabel="2011" itemValue="2011" />
<f:selectItem itemLabel="2012" itemValue="2012" />
<f:selectItem itemLabel="2013" itemValue="2013" />
<f:selectItem itemLabel="2014" itemValue="2014" />
<f:selectItem itemLabel="2015" itemValue="2015" />
</h:selectOneMenu>
</td>
</tr>
<tr>
<td><p:outputLabel for="TIPEMOTOR" value="Tipe Motor"/></td>
<td>
<h:selectOneMenu id="TIPEMOTOR" value="#{tipemotor.selectedVCLRDESC}" >
<f:selectItem itemLabel="Select One" itemValue="" />
<f:selectItems value="#{tipemotor.allTipeMotors}" />
</h:selectOneMenu>
</td>
</tr>
<tr>
<td width="200"><p:outputLabel for="kilometer" value="Kilometer" /></td>
<td width="500"><h:inputText value="#{customer.KILOMETER}" id="kilometer"/></td>
</tr>
<tr>
<td><h:outputLabel value="Nama:"/></td>
<td><h:inputText value="#{customer.VNAME}" id="VNAME"/></td>
</tr>
<tr>
<td><h:outputLabel value="Alamat:"/></td>
<td><h:inputText value="#{customer.VADRESS}" id="VADRESS"/></td>
</tr>
<tr>
<td><h:outputLabel value="No. HP:"/></td>
<td><h:inputText value="#{customer.NPHONENUM}" id="NPHONENUM"/></td>
</tr>
<tr>
<td><h:outputLabel value="Foto:"/></td>
<td><p:fileUpload value="#{customer.VIMAGE}" mode="simple" /></td>
</tr>
</table>
</h:form>
</pm:content>
</pm:page> </h:body>
</f:view>
我的问题是我无法使用ID为TIPEMOTOR的SelectOneMenu中的表ahmdsmun_msttypeclr显示数据。这怎么可以解决? 你的回答非常感谢