java 休眠异常:无法解析映射文档

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1618925/
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-10-29 17:18:31  来源:igfitidea点击:

Hibernate Exception: Could not parse mapping document

javahibernate

提问by

I use netbeans 6.7.1 to write "hello world" with hibernate, but I get some errors, please help me, thank you very much.

我使用netbeans 6.7.1 用hibernate 写“hello world”,但是出现了一些错误,请帮帮我,非常感谢。

my exception

我的例外

[[Exception in thread "main" org.hibernate.InvalidMappingException: Could not parse mapping document from resource hibernate/Tbluser.hbm.xml]]

[[线程“main”org.hibernate.InvalidMappingException 中的异常:无法从资源 hibernate/Tbluser.hbm.xml 解析映射文档]]

init:
deps-module-jar:
deps-ear-jar:
deps-jar:
Copying 1 file to F:\Documents and Settings\My Dropbox\DropboxNetBeanProjects\loginspring\build\web\WEB-INF\classes
compile-single:
run-main:
Oct 25, 2009 2:44:05 AM org.hibernate.cfg.Environment <clinit>
INFO: Hibernate 3.2.5
Oct 25, 2009 2:44:05 AM org.hibernate.cfg.Environment <clinit>
INFO: hibernate.properties not found
Oct 25, 2009 2:44:05 AM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: Bytecode provider name : cglib
Oct 25, 2009 2:44:05 AM org.hibernate.cfg.Environment <clinit>
INFO: using JDK 1.4 java.sql.Timestamp handling
Oct 25, 2009 2:44:05 AM org.hibernate.cfg.Configuration configure
INFO: configuring from resource: /hibernate.cfg.xml
Oct 25, 2009 2:44:05 AM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: Configuration resource: /hibernate.cfg.xml
Oct 25, 2009 2:44:06 AM org.hibernate.cfg.Configuration addResource
INFO: Reading mappings from resource : hibernate/Tbluser.hbm.xml
Oct 25, 2009 2:44:06 AM org.hibernate.util.XMLHelper$ErrorLogger error
SEVERE: Error parsing XML: XML InputStream(1) Document is invalid: no grammar found.
Oct 25, 2009 2:44:06 AM org.hibernate.util.XMLHelper$ErrorLogger error
SEVERE: Error parsing XML: XML InputStream(1) Document root element "hibernate-mapping", must match DOCTYPE root "null".
Exception in thread "main" org.hibernate.InvalidMappingException: Could not parse mapping document from resource hibernate/Tbluser.hbm.xml
        at org.hibernate.cfg.Configuration.addResource(Configuration.java:569)
        at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1587)
        at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1555)
        at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1534)
        at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1508)
        at org.hibernate.cfg.Configuration.configure(Configuration.java:1428)
        at org.hibernate.cfg.Configuration.configure(Configuration.java:1414)
        at hibernate.CreateTest.main(CreateTest.java:22)
Caused by: org.hibernate.InvalidMappingException: Could not parse mapping document from invalid mapping
        at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:502)
        at org.hibernate.cfg.Configuration.addResource(Configuration.java:566)
        ... 7 more
Caused by: org.xml.sax.SAXParseException: Document is invalid: no grammar found.
        at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:195)
        at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:131)
        at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:384)
        at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:318)
        at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:250)
        at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl$NSContentDriver.scanRootElementHook(XMLNSDocumentScannerImpl.java:626)
        at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:3095)
        at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:921)
        at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:648)
        at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:140)
        at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:510)
        at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:807)
        at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
        at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:107)
        at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1205)
        at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:522)
        at org.dom4j.io.SAXReader.read(SAXReader.java:465)
        at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:499)
        ... 8 more
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)

hibernate.cfg.xml

休眠文件.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.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate</property>
    <property name="hibernate.connection.username">root</property>
  </session-factory>
</hibernate-configuration>

Tbluser.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">
<!-- Generated Oct 25, 2009 2:37:30 AM by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
    <class name="hibernate.Tbluser" table="tbluser" catalog="hibernate">
        <id name="userId" type="java.lang.Integer">
            <column name="userID" />
            <generator class="identity" />
        </id>
        <property name="username" type="string">
            <column name="username" length="50" />
        </property>
        <property name="password" type="string">
            <column name="password" length="50" />
        </property>
        <property name="email" type="string">
            <column name="email" length="50" />
        </property>
        <property name="phone" type="string">
            <column name="phone" length="50" />
        </property>
        <property name="groupId" type="java.lang.Integer">
            <column name="groupID" />
        </property>
    </class>
</hibernate-mapping>

Tbluser.java

应用程序

  package hibernate;
// Generated Oct 25, 2009 2:37:30 AM by Hibernate Tools 3.2.1.GA



    /**
     * Tbluser generated by hbm2java
     */
    public class Tbluser  implements java.io.Serializable {


         private Integer userId;
         private String username;
         private String password;
         private String email;
         private String phone;
         private Integer groupId;

        public Tbluser() {
        }

        public Tbluser(String username, String password, String email, String phone, Integer groupId) {
           this.username = username;
           this.password = password;
           this.email = email;
           this.phone = phone;
           this.groupId = groupId;
        }

        public Integer getUserId() {
            return this.userId;
        }

        public void setUserId(Integer userId) {
            this.userId = userId;
        }
        public String getUsername() {
            return this.username;
        }

        public void setUsername(String username) {
            this.username = username;
        }
        public String getPassword() {
            return this.password;
        }

        public void setPassword(String password) {
            this.password = password;
        }
        public String getEmail() {
            return this.email;
        }

        public void setEmail(String email) {
            this.email = email;
        }
        public String getPhone() {
            return this.phone;
        }

        public void setPhone(String phone) {
            this.phone = phone;
        }
        public Integer getGroupId() {
            return this.groupId;
        }

        public void setGroupId(Integer groupId) {
            this.groupId = groupId;
        }


}

回答by SivaGanesh

In hibernate configuration file you missed out the mapping element for your hbm file.you should define like in your hibernate.cfg.xml..

在 hibernate 配置文件中,您错过了 hbm 文件的映射元素。您应该在 hibernate.cfg.xml 中定义类似的内容。

回答by e2rabi

I think that you have to add a mapping for your Tbluser.hbm.xml in your hibernate cfg

我认为你必须在你的 hibernate cfg 中为你的 Tbluser.hbm.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.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate</property>
        <property name="hibernate.connection.username">root</property>

  <!-- List of XML mapping files -->
   <mapping resource="Tbluser.hbm.xml"/>
      </session-factory>
    </hibernate-configuration>

回答by Jens Schauder

It sounds like the xml parser is trying to access the schema or dtd mentioned in the configuration file and fails to succeed.

听起来 xml 解析器正在尝试访问配置文件中提到的架构或 dtd,但未能成功。

Make sure your application has access to http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtdor get the document from there put it somewhere where your application can access it add change the path in the document accordingly

确保您的应用程序可以访问http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd或从那里获取文档 将它放在您的应用程序可以访问它的某个地方 添加相应地更改文档中的路径