0

I am using NetbeansIDE 7.2. and pgAdmin III PostgreSQL 9.2 with Database name "StudentReports".

I know a little of Hibernate and generating JasperReports using native SQL through JDBC Connection, but I am new to generating JasperReports connecting thru Hibernate Connection which this tutorial shows.

I am getting org.hibernate.InvalidMappingException: Unable to read XML Error and never made a successful Hibernate Connection test, an error pops up saying "Could not parse mapping document from resource com/report/mappings/Department.hbm.xml", after following this tutorial "JasperReports with Hibernate - Module 1" and JasperReports with Hibernate - Module 2. It has 2 Modules. But I did a little modifications on its data access object (DAO) files and I just wanted to have a Department Records. Also, I used a different version of Hibernate and Jasper Reports Package and iReport Plugin for Netbeans which are Hibernate Release 4.1.9 and Jasper Reports 5.0.1 and iReport-4.5.0. I have added all of the jar files of it to my project's Library.

This is my first time handling hibernate mapping xml files and data access object files.

log4j:WARN No appenders could be found for logger (org.jboss.logging). log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" org.hibernate.InvalidMappingException: Unable to read XML at org.hibernate.internal.util.xml.MappingReader.readMappingDocument(MappingReader.java:109) at org.hibernate.cfg.Configuration.add(Configuration.java:478) at org.hibernate.cfg.Configuration.add(Configuration.java:474) at org.hibernate.cfg.Configuration.add(Configuration.java:647) at org.hibernate.cfg.Configuration.addResource(Configuration.java:730) at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:2115) at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:2087) at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2067) at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2020) at org.hibernate.cfg.Configuration.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) Caused by: org.dom4j.DocumentException: Error on line 15 of document : The element type "hibernate-mapping" must be terminated by the matching end-tag "". Nested exception: The element type "hibernate-mapping" must be terminated by the matching end-tag "". at org.dom4j.io.SAXReader.read(SAXReader.java:482) at org.hibernate.internal.util.xml.MappingReader.readMappingDocument(MappingReader.java:78) ... 12 more Java Result: 1

These are my codes:

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 Answers 2

5

The error shows

The element type "hibernate-mapping" must be terminated by the matching end-tag

Therefore add a matching closing tag for this element in Department.hbm.xml as expected by the XML parser

</hibernate-mapping>

Similarly add a closing hibernate-configuration element in hibernate.cfg.xml

</hibernate-configuration>
Sign up to request clarification or add additional context in comments.

1 Comment

I see. It had actually solved the error. Thank you so much. I I am now connected to my Hibernate Connection.
0

Sometimes is because you have a break line in the start of XML file.

<!--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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.