当前位置: 代码迷 >> J2EE >> hibernate.MappingException: Error reading resource: zhb/ssh/entity/zhb.hbm.xml解决方案
  详细解决方案

hibernate.MappingException: Error reading resource: zhb/ssh/entity/zhb.hbm.xml解决方案

热度:354   发布时间:2016-04-22 01:50:52.0
hibernate.MappingException: Error reading resource: zhb/ssh/entity/zhb.hbm.xml

hibernate.cfg.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC  
  "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
  "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
  <hibernate-configuration>
  <session-factory>
  <property name="dialect">org.hibernate.dialect.OracleDialect</property>
  <property name="connection.url">jdbc:oracle:thin:@192.168.20.2:1521:rjgc</property>
  <property name="connection.username">interread</property>
  <property name="connection.password">interread</property>
  <property name="connection.driver">oracle.jdbc.driver.OracleDriver</property>
  <property name="show_sql">true</property>
  <mapping resource="zhb/ssh/entity/zhb.hbm.xml"/>
   
  </session-factory>
   
  </hibernate-configuration>

zhb.hbm.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Hibernate-mapping PUBLIC
 "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
 <hibernate-mapping>
 <class name="zhb.ssh.entity.ZhbBean" table="ZHB">
 <id name="id" type="java.lang.Integer">
 <column name="ID"/>
 <generator class="assigned"/>
 </id>
 <property name="name" type="java.lang.String">
 <column name="NAME" length="20"></column>
 </property>
 <property name="typeId" type="java.lang.Integer">
 <column name="TYPEID"></column>
 </property>
 </class>
 </hibernate-mapping>

ZhbBean类

package zhb.ssh.entity;

import java.io.Serializable;

public class ZhbBean implements Serializable {

private int id;
private String name;
private int typeId;

public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getTypeId() {
return typeId;
}
public void setTypeId(int typeId) {
this.typeId = typeId;
}

}

执行的Java类:
package zhb.ssh.dao;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import zhb.ssh.entity.ZhbBean;

public class ZhbDao {

  public ZhbBean getZhb(int id){
  SessionFactory sf=new Configuration().configure().buildSessionFactory();
Session session=sf.openSession();
ZhbBean zb=(ZhbBean)session.get(ZhbBean.class, id);
sf.close();
return zb;
}
  public static void main(String[] args){
ZhbDao z=new ZhbDao();
ZhbBean zhb= z.getZhb(2);
System.out.println(zhb.getName());
  }
}


错误信息:

1、org.hibernate.MappingException: Error reading resource: zhb/ssh/entity/zhb.hbm.xml

2、org.hibernate.MappingException: invalid mapping

3、org.xml.sax.SAXParseException: Document root element "hibernate-mapping", must match DOCTYPE root "Hibernate-mapping".
  相关解决方案