Hibernate:读取hbm.xml

时间:2012-03-27 10:12:39

标签: java xml hibernate

我正在关注roseindia的这个知识产生以获得Hibernate的基础知识:“http://roseindia.net/hibernate/hibernate-update.shtml”

我的代码如下所示并获得相同的错误。请帮我修理一下!

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”

此外,我还使用这些列字段创建了名为Insurance的数据库表。

感谢
斯纳

3 个答案:

答案 0 :(得分:0)

hbm.xml文件中是否缺少某些内容?它不是一个完整的XML文件。

您需要将hbm.xml文件与您的类的已编译类文件一起放置。

答案 1 :(得分:0)

这是你的整个.hbm.xml文件吗?如果是这样,它就不完整了 - 它缺少这里显示的正确结构:http://roseindia.net/hibernate/hibernateormapping.shtml

<?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="roseindia.tutorial.hibernate.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>
</hibernate-mapping>

答案 2 :(得分:0)

你必须定义两个POJO类

    <?xml version="1.0" encoding="UTF-8"?>
        <!DOCTYPE hibernate-mapping
            PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
                  "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">                      
                <hibernate-mapping>

                    <class name="Book" table="book" >          
                             <id name="lngBookId" column="id" type="long">
                            <generator class="increment"></generator>
                         </id>
                            <property name="strBookName" type="string">
                               <column name="bookname" sql-type="VARCHAR2(55)"/>
                            </property>

                    </class>                   

                <class name="Insurance" table="insurance" >          

                 <property name="firstName" type="string">
                      <column name="FIRSTNAME" sql-type="VARCHAR2(55)"/>
                 </property>
                 <property name="lastName" type="string">
                      <column name="LASTNAME" sql-type="VARCHAR2(55)"/>
                 </property>
                 <property name="email" type="string">
                      <column name="EMAIL" sql-type="VARCHAR2(55)"/>
                 </property>
            </class>
            </hibernate-mapping>