获取org.hibernate.InvalidMappingException:无法读取XML



我正在使用NetbeansIDE 7.2。和pgAdmin III PostgreSQL 9.2与数据库名称"StudentReports".

我对Hibernate和通过JDBC连接使用本地SQL生成JasperReports有一点了解,但是我对通过Hibernate连接生成JasperReports还是个新手。

我得到0 rg.hibernate.InvalidMappingException: Unable to read XML Errornever做了一个成功的Hibernate连接测试,一个错误弹出说"无法解析映射文档从资源com/report/mappings/Department.hbm.xml",在遵循本教程"JasperReports与Hibernate -模块1"和JasperReports与Hibernate -模块2。它有两个模块。但是我对它的数据访问对象(DAO)文件做了一点修改,我只想要一个Department Records。此外,我为Netbeans使用了不同版本的Hibernate和Jasper报告包和iReport插件,其中Hibernate发布4.1.9,Jasper报告5.0.1和iReport-4.5.0。我已经将它的所有jar文件添加到我的项目库中。

这是我第一次处理hibernate映射xml文件和数据访问对象文件。

log4j:WARN没有appeners for logger (org.jboss.logging)。请正确初始化log4j系统。
线程"main"中的异常:无法读取XMLorg.hibernate.internal.util.xml.MappingReader.readMappingDocument (MappingReader.java: 109)org.hibernate.cfg.Configuration.add (Configuration.java: 478)org.hibernate.cfg.Configuration.add (Configuration.java: 474)org.hibernate.cfg.Configuration.add (Configuration.java: 647)org.hibernate.cfg.Configuration.addResource (Configuration.java: 730)org.hibernate.cfg.Configuration.parseMappingElement (Configuration.java: 2115)org.hibernate.cfg.Configuration.parseSessionFactory (Configuration.java: 2087)org.hibernate.cfg.Configuration.doConfigure (Configuration.java: 2067)org.hibernate.cfg.Configuration.doConfigure (Configuration.java: 2020)org.hibernate.cfg.Configuration.configure (Configuration.java: 1935)org.hibernate.cfg.Configuration.configure (Configuration.java: 1914)com.report.dao.DepartmentDAO.saveDepartment (DepartmentDAO.java: 20)com.report.test.AddDepartments.main (AddDepartments.java: 20)原因:org.dom4j。DocumentException: document的第15行错误:元素类型"hibernate-mapping"必须以匹配的结束标签"结束。嵌套异常:元素类型"hibernate-mapping"必须以匹配的结束标签"结束。org.dom4j.io.SAXReader.read (SAXReader.java: 482)org.hibernate.internal.util.xml.MappingReader.readMappingDocument (MappingReader.java: 78)…12Java Result: 1

这些是我的代码:

Department.java

package com.report.beans;
  public class Department  implements java.io.Serializable {
 private int id;
 private String name;
public long getId() {
    return this.id;
}
public void setId(int id) {
    this.id = id;
}
public String getName() {
    return this.name;
}
public void setName(String name) {
    this.name = name;
}
}

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="hibernate.connection.driver_class">org.postgresql.Driver</property>
  <property name="hibernate.connection.url"> jdbc:postgresql://localhost:5432/StudentReports</property>
  <property name="hibernate.connection.username">postgres</property>
  <property name="hibernate.connection.password">postgres</property>
  <property name="hibernate.connection.pool_size">1</property>
  <property name="hibernate.show_sql">true</property>
  <property name="hibernate.cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
   <property name="hibernate.hbm2ddl.auto">update</property>    
<mapping resource="com/report/mappings/Department.hbm.xml"/>
</session-factory>

Department.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="com.report.beans.Department"  lazy="false" table="Department" schema="dbo"      catalog="StudentReports"/>
        <id name="id" type="long">
        <column name="ID" />
        <generator class="increment" />
    </id>
    <property name="name" type="string">
        <column name="name" length="100" not-null="true" unique="true" />
    </property>


DepartmentDAO.java

package com.report.dao;
import com.report.beans.Department;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class DepartmentDAO{

public String saveDepartment(Department department)
{
    SessionFactory sessionF = new Configuration().configure().buildSessionFactory();
    Session session = sessionF.openSession();
//  This is the code I modified. I didn'y use HibernateSessionFactory
//  Session session = HibernateSessionFactory.getSession();
    String Result = "";
    try
    {
        session.beginTransaction();
        session.save(department);
        session.getTransaction().commit();
        session.close();
        Result = "Department Saved Successfully";
    }
    catch(Exception e)
    {
        e.printStackTrace();
        session.close();
        Result = "Department was not saved due to the above Exception";
    }
    return Result;
}
}

AddDepartments.java

package com.report.test;
import com.report.beans.Department;
import com.report.dao.DepartmentDAO;
public class AddDepartments
{
public static void main(String args[])
{
    Department electronics = new Department();
        electronics.setName("Electronics Engineering");
    Department computerScience = new Department();
        computerScience.setName("Computer science Engineering");
    Department civil = new Department();
        civil.setName("Civil Engineering");

    String Result1 = new DepartmentDAO().saveDepartment(electronics);
        System.out.println(Result1);
    String Result2 = new DepartmentDAO().saveDepartment(computerScience);
        System.out.println(Result2);
    String Result3 = new DepartmentDAO().saveDepartment(civil);
        System.out.println(Result3);
}
}

错误显示

元素类型"hibernate-mapping"必须以匹配的结束标签

结束

因此,按照XML解析器

的期望,在Department.hbm.xml中为该元素添加一个匹配的结束标记
</hibernate-mapping>

同样在hibernate.cfg.xml

中添加结束的hibernate-configuration元素
</hibernate-configuration>

有时是因为在XML文件的开头有一个换行符。

<!--Sure that no exists break line here.-->
<?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="hibernate.Person" table="PEOPLES">
        <id name="id" column="ID">
            <generator class="foreign"> 
            <param name="property">adress</param> 
        </generator>
        </id>
        <property name="name" /> 
        <one-to-one name="adress" cascade="persist, delete"/> 
    </class> 
</hibernate-mapping>

相关内容

  • 没有找到相关文章

最新更新