2012-03-27 59 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>