我下面这个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