0

我正在使用 NetbeansIDE 7.2。和数据库名称为“StudentReports”的 pgAdmin III PostgreSQL 9.2。

我对 Hibernate 和通过 JDBC Connection 使用本机 SQL 生成 JasperReports 有一点了解,但是对于通过本教程显示的 Hibernate Connection 连接生成 JasperReports 是新手。

我得到了 org.hibernate.InvalidMappingException: Unable to read XML Error并且从未成功进行过 Hibernate 连接测试,在遵循本教程“JasperReports with Hibernate - Module 1 ”JasperReports 与 Hibernate - 模块 2。它有 2 个模块。但我对其数据访问对象 (DAO) 文件进行了一些修改,我只想拥有一个部门记录。此外,我为 Netbeans 使用了不同版本的 Hibernate 和 Jasper Reports 包和 iReport 插件,它们是 Hibernate Release 4.1.9 和 Jasper Reports 5.0.1 和 iReport-4.5.0。我已将它的所有 jar 文件添加到我的项目库中。

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

log4j:WARN 找不到记录器 (org.jboss.logging) 的附加程序。log4j:WARN 请正确初始化 log4j 系统。
configure(Configuration.java:1935) at org.hibernate.cfg.Configuration.configure(Configuration.java:1914) at com.report.dao.DepartmentDAO.saveDepartment(DepartmentDAO.java:20) at 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 report.dao.DepartmentDAO.saveDepartment(DepartmentDAO.java:20) at 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 report.dao.DepartmentDAO.saveDepartment(DepartmentDAO.java:20) at 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

这些是我的代码:

部门.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;
}
}

休眠.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>

部门.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>


部门DAO.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;
}
}

添加部门.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);
}
}
4

2 回答 2

5

错误显示

元素类型“hibernate-mapping”必须由匹配的结束标签终止

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

</hibernate-mapping>

同样hibernate-configurationhibernate.cfg.xml

</hibernate-configuration>
于 2013-06-15T12:36:36.713 回答
0

有时是因为您在 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>
于 2018-02-02T16:44:48.557 回答