Org.hibernate.InvalidMappingException: Failed to parse the display of the document from * .hbm.xml

I know that many people ask this question, but I read almost everything, but not one of them helped me. I am writing an eclipse maven project with hibernate and I get this error:

org.hibernate.InvalidMappingException: Could not parse mapping document from resource ir/ac/ut/ieproj/da/Student.hbm.xml 

my files look like this:

pom.xml

  <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>ir.ac.ut</groupId> <artifactId>ieproj</artifactId> <version>0.2</version> <packaging>war</packaging> <name>ieproj</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <build> <resources> <resource> <directory>src/main/java</directory> <filtering>true</filtering> </resource> </resources> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>2.0</version> </plugin> <plugin> <artifactId>maven-assembly-plugin</artifactId> <version>2.2</version> <configuration> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> </plugins> </build> <repositories> <repository> <id>JBoss repository</id> <url>http://repository.jboss.org/nexus/content/groups/public/</url> </repository> </repositories> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>ir.ac.ut</groupId> <artifactId>iecommon</artifactId> <version>1.0</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.24</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> <version>3.5.1-Final</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>3.5.1-Final</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.4</version> </dependency> <dependency> <groupId>dom4j</groupId> <artifactId>dom4j</artifactId> <version>1.6.1</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-validator</artifactId> <version>5.0.1.Final</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate</artifactId> <version>3.5.4-Final</version> <type>pom</type> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.4</version> </dependency> </dependencies> </project> 

hibernate.cfg.xml

  <?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/db</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">13812002</property> <property name="hibernate.connection.pool_size">10</property> <property name="show_sql">true</property> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.hbm2ddl.auto">update</property> <!-- Mapping files --> <mapping resource="ir/ac/ut/ieproj/da/Department.hbm.xml"/> <mapping resource="ir/ac/ut/ieproj/da/Studyrec.hbm.xml"/> <mapping resource="ir/ac/ut/ieproj/da/Student.hbm.xml"/> </session-factory> </hibernate-configuration> 

Department.hbm.xml

 <?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 package="ir.ac.ut.ieproj.da"> <class name="DepartmentRepo" table="department"> <id name="id" type="int" column="ID" > <generator class="assigned"/> </id> <property name="name" column="Name" type="string"/> </class> <sql-query name="getDeptName"> <return alias="Department" class="DepartmentRepo"/> <![CDATA[select * from db.department d where d.ID = :id]]> </sql-query> </hibernate-mapping> 

Student.hbm.xml

 <?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 package="ir.ac.ut.ieproj.da"> <class name="StudentRepo" table="student"> <id name="id" type="int" column="ID" > <generator class="assigned"/> </id> <property name="firstName" type="string" column="FirstName" /> </property> <property name="lastName" type="string" column="LastName"/> </property> <property name="program" type="string" column="Program"/> </property> <many-to-one name="dept" class="DepartmentRepo" cascade="all" not-null="true" column="deptId"/> </class> <sql-query name="findStudentId"> <return alias="Student" class="StudentRepo"/> <![CDATA[select * from db.student s where s.ID = :sid]]> </sql-query> </hibernate-mapping> 

StudentRepo.java

 package ir.ac.ut.ieproj.da; import ir.ac.ut.ieproj.model.Student; import org.hibernate.HibernateException; import org.hibernate.MappingException; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.Transaction; public class StudentRepo { private int id; private String firstName; private String lastName; private String program; private DepartmentRepo dept; public Student getStudentbyId (String sid) throws MappingException, HibernateException, Exception { Student student = new Student(); Session session = HibernateUtil.getHibernateSession(); Transaction tx = session.beginTransaction(); Query query = session.getNamedQuery("findStudentId").setLong("sid", Long.valueOf(sid)); StudentRepo studentRepo = (StudentRepo) query.uniqueResult(); student.setId(studentRepo.getId()); student.setFirstName(studentRepo.getFirstName()); student.setLastName(studentRepo.getLastName()); student.setProgram(Integer.valueOf(studentRepo.getProgram())); tx.commit(); session.close(); return student; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getProgram() { return program; } public void setProgram(String program) { this.program = program; } public DepartmentRepo getDept() { return dept; } public void setDept(DepartmentRepo dept) { this.dept = dept; } } 

DepartmentRepo.java

 package ir.ac.ut.ieproj.da; import org.hibernate.HibernateException; import org.hibernate.MappingException; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.Transaction; import ir.ac.ut.ieproj.model.Department; public class DepartmentRepo { private int id; private String name; public Department getDeptbyId(String id) throws MappingException, HibernateException, Exception { Session session = HibernateUtil.getHibernateSession(); Transaction tx = session.beginTransaction(); Query query = session.getNamedQuery("getDeptName").setLong("id", Integer.valueOf(id)); DepartmentRepo departmentRepo = (DepartmentRepo) query.uniqueResult(); Department department = new Department(); department.setName(departmentRepo.getName()); tx.commit(); session.close(); return department; } 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; } } 

I think the problem is with the many-to-one tag in Student.hbm.xml, because Department.hbm.xml and namedQuery do not cause errors inside. What am I doing wrong? I use the mvn package to create the .war file and then deploy it to tomcat 7.

+4
source share
8 answers

The error Could not parse mapping document is due to the fact that your xml files are not correctly formed. When this error occurs, we better check our xml files to make sure they are in order (all tags are closed correctly, etc.).


In your case, as the message says, your Student.hbm.xml file is the problem. You have tags that do not belong:

 <property name="firstName" type="string" column="FirstName" /> </property> <------------------------------------------------------ remove this <property name="lastName" type="string" column="LastName"/> </property> <------------------------------------------------------ remove this <property name="program" type="string" column="Program"/> </property> <------------------------------------------------------ remove this 

Those closing tags </property> do not close anyone, since the <property tags above them are self-closing (pay attention to /> ).

+8
source

Try changing the display of the DTD sleep mode. My problem is solved after changing http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd to http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd

+1
source

This problem may be due to a mismatch of the variable names used in the bean class.

For example, then your variable names should be FirstName.

0
source

In my case, it was just a comment at the beginning of xml. If anyone has a comment, just delete it and maybe it will work.

0
source

Remove <?xml version="1.0"?> From your mapping and configuration file. He will solve your problem.

0
source

you could set up a matching tag several times for the same object / persistence.

0
source

My problem is solved after changing http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd to http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd . The problem may be caused by a timeout while accessing the remote DTD file.

I would download hibernate-mapping-3.0.dtd and make it available locally.

0
source

You just close your property, for example

 <property name="firstName" type="string" column="FirstName" /> 

or

 <property name="firstName" type="string" column="FirstName"></property> 
-1
source

Source: https://habr.com/ru/post/1481587/


All Articles