2012-03-27 37 views
0

我下面這個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」

的名字保險與列字段還我已創建數據庫表。

感謝
斯納

回答

0

是不是在hbm.xml文件中缺少什麼?它不是一個完整的XML文件。

您需要將hbm.xml文件與編譯後的類文件放在一起。

+0

是什麼呢?缺 – Smitha 2012-03-27 10:24:57

+0

強制「id」應該是int類型還是long類型? – Smitha 2012-03-27 10:28:50

+0

你需要做兩件事:1)確保你想要映射的每個類都有一個hbm.xml文件(而不是在一個文件中映射多個類),並且2)確保你的XML是格式良好(你有一個額外的>後保險)。 – 2012-03-27 10:45:08

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> 
+0

是的..是啊..我跟着那個hbm.xml結構..某些方面,它不反映在這裏..我編輯n更新它... – Smitha 2012-03-27 10:23:43

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>