eclipse org.hibernate.InvalidMappingException:无法从资源 *.hbm.xml 解析映射文档

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16631982/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 21:43:05  来源:igfitidea点击:

org.hibernate.InvalidMappingException: Could not parse mapping document from resource *.hbm.xml

eclipsehibernatemavenmany-to-one

提问by Raein Hashemi

I know this question has been asked a lot, but I read almost every one of them but non of them helped me. I'm writing an eclipse maven project with hibernate and I'm getting this error:

我知道这个问题已经被问了很多,但我几乎阅读了其中的每一个,但没有一个对我有帮助。我正在使用 hibernate 编写 eclipse maven 项目,但出现此错误:

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

my files are like this:

我的文件是这样的:

pom.xml

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

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

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

学生.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

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

部门仓库.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 the namedQuery within are not causing an error. What am I doing wrong? I'm using mvn package to make a .war file and then deploy it in tomcat 7.

我认为问题在于 Student.hbm.xml 中的多对一标记,因为 Department.hbm.xml 和其中的 namedQuery 不会导致错误。我究竟做错了什么?我正在使用 mvn 包制作一个 .war 文件,然后将其部署在 tomcat 7 中。

回答by acdcjunior

The error Could not parse mapping documentis about your xml files notbeing well-formed. When this error comes up, we'd better double check our xml files to see if they are really OK (all tags are properly closed and so on).

错误Could not parse mapping document是关于您的 xml 文件格式不正确。当这个错误出现时,我们最好仔细检查我们的 xml 文件,看看它们是否真的正常(所有标签都正确关闭等等)。



In your case, as the message states, your Student.hbm.xmlfile is the problem. You have some tags that do not belong:

在您的情况下,正如消息所述,您的Student.hbm.xml文件是问题所在。您有一些不属于的标签:

<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 </property>tags aren't closing no one, as the <propertytags above them are self-closed (notice the />).

那些结束</property>标签不会关闭任何人,因为<property它们上方的标签是自闭合的(注意/>)。

回答by Rajshree Karangale

回答by iLearn World

My problem solves after changing http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtdto http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd. The issue could caused by timeout when access to remote DTD file.

http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd更改为http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd后,我的问题解决了。该问题可能是由于访问远程 DTD 文件时超时造成的。

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

我会下载 hibernate-mapping-3.0.dtd 并使其在本地可用。

回答by Krishna.p

May Be This problem because of Not matching with The variable names used in bean class.

可能是这个问题,因为与 bean 类中使用的变量名不匹配。

For eg. Then Your variable names should be FirstName.

例如。那么你的变量名应该是名字。

回答by Edvan Souza

In my case was just a comment at the beggining of xml. If someone have a comment like just remove it and maybe will work.

在我的情况下只是在 xml 开始时的评论。如果有人有评论,例如将其删除,也许会起作用。

回答by Saumendra

Please remove <?xml version="1.0"?>from your mapping and config file.It will solve your problem

<?xml version="1.0"?>从您的映射和配置文件中删除。它将解决您的问题

回答by Ajay Takur

you might have configured multiple times of mapping tag for same entity/persistence.

您可能为同一实体/持久性配置了多次映射标记。

回答by SANTOSH BIRADAR

You just close your property like

你只需关闭你的财产就像

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

or

或者

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