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

时间:2013-06-15 07:22:15

标签: hibernate netbeans hibernate-mapping

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

我知道一点Hibernate和通过JDBC连接使用本机SQL生成JasperReports的,但我的新生成连接直通休眠连接的JasperReports其中这个教程所示。

我正在-O rg.hibernate.InvalidMappingException: Unable to read XML Error,并从未进行了成功的休眠连接测试,错误弹出说"无法从解析映射文档资源COM /报告/映射/遵循本教程"JasperReports with Hibernate - Module 1"JasperReports with Hibernate - Module 2之后的Department.hbm.xml"。它有2个模块。但我对其数据访问对象(DAO)文件做了一些修改,我只想要一个部门记录。另外,我用了不同版本的Hibernate和Jasper报表包装和iReport的插件的NetBeans这是Hibernate的版本4.1.9和贾斯珀报告5.0.1和iReport的-4.5.0。我已将它的所有jar文件添加到我的项目库中。

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

  

log4j:WARN找不到logger(org.jboss.logging)的appender。   log4j:WARN请正确初始化log4j系统   线程" main"中的例外情况org.hibernate.InvalidMappingException:无法读取XML       在org.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:文档第15行出错:元素类型" hibernate-mapping"必须由匹配的结束标记终止""。嵌套异常:元素类型" hibernate-mapping"必须由匹配的结束标记终止""。       在org.dom4j.io.SAXReader.read(SAXReader.java:482)       在org.hibernate.internal.util.xml.MappingReader.readMappingDocument(MappingReader.java:78)       ......还有12个   Java结果: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);
}
}

2 个答案:

答案 0 :(得分:4)

错误显示

  

元素类型&#34; hibernate-mapping&#34;必须由匹配的结束标记

终止

因此,按照XML解析器的预期,在Department.hbm.xml中为此元素添加匹配的结束标记

</hibernate-mapping>

同样在hibernate-configuration

中添加结束hibernate.cfg.xml元素
</hibernate-configuration>

答案 1 :(得分:0)

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

{
  "name": "cb-assignment",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "node app.js",
    "test": "mocha --timeout 10000 --exit"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "body-parser": "^1.18.2",
    "chai": "^4.1.2",
    "chai-http": "^3.0.0",
    "easyimage": "^3.0.0",
    "express": "^4.16.2",
    "express-session": "^1.15.6",
    "imagemagick": "^0.1.3",
    "jsonpatch": "^3.0.1",
    "jsonwebtoken": "^8.1.1",
    "mocha": "^5.0.0",
    "node-image-resize": "0.0.2",
    "request": "^2.83.0"
  },
  "devDependencies": {
    "istanbul": "^0.4.5"
  }
}