我下面這個titorial上roseindia獲得休眠的基礎:「http://roseindia.net/hibernate/hibernate-update.shtml」休眠:閱讀的hbm.xml
我的代碼的下方,讓錯誤是一樣的。請幫助我解決它!
Java代碼:
public class UpdateExample {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Session sess = null;
try {
SessionFactory fact = new Configuration().configure().buildSessionFactory();
sess = fact.openSession();
Transaction tr = sess.beginTransaction();
Insurance ins = (Insurance)sess.get(Insurance.class, new Long(1));
ins.setInsuranceName("Jivan Dhara");
ins.setInvestementAmount(20000);
ins.setInvestementDate(new Date());
sess.update(ins);
tr.commit();
sess.close();
System.out.println("Update successfully!");
}
catch(Exception e){
System.out.println(e.getMessage());
}
}
}
而且
public class Insurance {
private String insuranceName;
private double investementAmount;
private Date investementDate;
public String getInsuranceName() {
return insuranceName;
}
public void setInsuranceName(String insuranceName) {
this.insuranceName = insuranceName;
}
public double getInvestementAmount() {
return investementAmount;
}
public void setInvestementAmount(double investementAmount) {
this.investementAmount = investementAmount;
}
public Date getInvestementDate() {
return investementDate;
}
public void setInvestementDate(Date investementDate) {
this.investementDate = investementDate;
}
}
而且我contact.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">
<hibernate-mapping>
<class name="Contact" table="CONTACT">
<id name="id" type="long" column="ID" >
<generator class="assigned"/>
</id>
<property name="firstName">
<column name="FIRSTNAME" />
</property>
<property name="lastName">
<column name="LASTNAME"/>
</property>
<property name="email">
<column name="EMAIL"/>
</property>
</class>
<class name="Book" table="book">
<id name="lngBookId" type="long" column="id" >
<generator class="increment"/>
</id>
<property name="strBookName">
<column name="bookname" />
</property>
</class>
<class name="Insurance" table="insurance">
<id name="insuranceName" type="String" column="InsuranceName" >
/>
</id>
<property name="investmentAmount">
<column name="InvestmentAmount" />
</property>
<property name="investmentDate">
<column name="InvestmentDate" />
</property>
</class>
</hibernate-mapping>
,我得到的錯誤是:
「錯誤閱讀資源:contact.hbm.xml」
的名字保險與列字段還我已創建數據庫表。
感謝
斯納
是什麼呢?缺 – Smitha 2012-03-27 10:24:57
強制「id」應該是int類型還是long類型? – Smitha 2012-03-27 10:28:50
你需要做兩件事:1)確保你想要映射的每個類都有一個hbm.xml文件(而不是在一個文件中映射多個類),並且2)確保你的XML是格式良好(你有一個額外的>後保險)。 – 2012-03-27 10:45:08