簡體   English   中英

休眠:讀取hbm.xml

[英]Hibernate : Reading hbm.xml

我正在遵循有關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”

我還用這些列字段通過保險創建了數據庫表。

謝謝
斯內哈

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

您需要將hbm.xml文件與已編譯的類文件一起放置為您的類。

這是您的整個.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>

您必須定義兩個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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM