【问题标题】:Hibernate : Reading hbm.xml休眠:读取 hbm.xml
【发布时间】:2012-03-27 10:12:39
【问题描述】:

我正在关注 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 的 db 表。

谢谢
斯内哈

【问题讨论】:

    标签: java xml hibernate


    【解决方案1】:

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

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

    【讨论】:

    • id必须是int型还是long型??
    • 您需要做 2 件事:1) 确保每个要映射的类都有一个 hbm.xml 文件(而不是在一个文件中映射多个类),以及 2 ) 确保您的 XML 格式正确(在 insuranceName 之后有一个额外的 >)。
    • id 不必是 int 或 long,但这通常是最简单的方法。
    • 是的..我已按照您的建议进行了修改..现在出现此错误:无法确定类型:字符串,列:[org.hibernate.mapping.Column(InsuranceName)] :(
    • 如果我使用“字符串”而不是字符串,它会说...getter 没有为 InsuranceName 定义...
    【解决方案2】:

    这是您的整个 .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>
    

    【讨论】:

    • 是的..我遵循了 hbm.xml 结构..不知何故,它没有反映在这里..让我编辑和更新它...
    【解决方案3】:

    你必须定义两个 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>
    

    【讨论】:

      猜你喜欢
      • 2013-06-23
      • 2010-12-29
      • 2013-08-14
      • 1970-01-01
      • 1970-01-01
      • 2010-09-20
      • 1970-01-01
      • 1970-01-01
      • 2013-05-10
      相关资源
      最近更新 更多