Java 休眠 4. 无效的映射异常。无法读取 XML。必须声明元素类型 hibernate-mapping

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

Hibernate 4. Invalid Mapping Exception. Unable to read XML. Element type hibernate-mapping must be declared

javaxmlhibernatejpa

提问by Petr Shypila

I have InvalidMappingExceptionon org.hibernate.cfg.Configuration.configure()method call.

InvalidMappingExceptionorg.hibernate.cfg.Configuration.configure()方法调用。

Stack trace:

堆栈跟踪:


javax.servlet.ServletException: org.hibernate.InvalidMappingException: Unable to read XML
    org.apache.struts.chain.ComposableRequestProcessor.process(ComposableRequestProcessor.java:286)
    org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
    org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:462)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

root cause

org.hibernate.InvalidMappingException: Unable to read XML
    org.hibernate.internal.util.xml.MappingReader.readMappingDocument(MappingReader.java:109)
    org.hibernate.cfg.Configuration.add(Configuration.java:488)
    org.hibernate.cfg.Configuration.add(Configuration.java:484)
    org.hibernate.cfg.Configuration.add(Configuration.java:657)
    org.hibernate.cfg.Configuration.addResource(Configuration.java:740)
    org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:2188)
    org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:2160)
    org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2140)
    org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2093)
    org.hibernate.cfg.Configuration.configure(Configuration.java:2008)
    org.hibernate.cfg.Configuration.configure(Configuration.java:1987)
    com.petrez.util.HibernateUtil.getSessionFactory(HibernateUtil.java:12)
    com.action.UserAction.setUser(UserAction.java:29)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:601)
    org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:269)
    org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:170)
    org.apache.struts.chain.commands.servlet.ExecuteAction.execute(ExecuteAction.java:58)
    org.apache.struts.chain.commands.AbstractExecuteAction.execute(AbstractExecuteAction.java:67)
    org.apache.struts.chain.commands.ActionCommandBase.execute(ActionCommandBase.java:51)
    org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:191)
    org.apache.commons.chain.generic.LookupCommand.execute(LookupCommand.java:305)
    org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:191)
    org.apache.struts.chain.ComposableRequestProcessor.process(ComposableRequestProcessor.java:283)
    org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
    org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:462)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

root cause

org.xml.sax.SAXParseException; systemId: ; lineNumber: 5; columnNumber: 47; Element type "hibernate-mapping" must be declared.
    org.apache.xerces.framework.XMLParser.reportError(XMLParser.java:1213)
    org.apache.xerces.validators.common.XMLValidator.reportRecoverableXMLError(XMLValidator.java:1807)
    org.apache.xerces.validators.common.XMLValidator.validateElementAndAttributes(XMLValidator.java:3633)
    org.apache.xerces.validators.common.XMLValidator.callStartElement(XMLValidator.java:1229)
    org.apache.xerces.framework.XMLDocumentScanner.scanElement(XMLDocumentScanner.java:1806)
    org.apache.xerces.framework.XMLDocumentScanner$ContentDispatcher.dispatch(XMLDocumentScanner.java:949)
    org.apache.xerces.framework.XMLDocumentScanner.parseSome(XMLDocumentScanner.java:381)
    org.apache.xerces.framework.XMLParser.parse(XMLParser.java:1098)
    org.dom4j.io.SAXReader.read(SAXReader.java:465)
    org.hibernate.internal.util.xml.MappingReader.readMappingDocument(MappingReader.java:78)
    org.hibernate.cfg.Configuration.add(Configuration.java:488)
    org.hibernate.cfg.Configuration.add(Configuration.java:484)
    org.hibernate.cfg.Configuration.add(Configuration.java:657)
    org.hibernate.cfg.Configuration.addResource(Configuration.java:740)
    org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:2188)
    org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:2160)
    org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2140)
    org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2093)
    org.hibernate.cfg.Configuration.configure(Configuration.java:2008)
    org.hibernate.cfg.Configuration.configure(Configuration.java:1987)
    com.petrez.util.HibernateUtil.getSessionFactory(HibernateUtil.java:12)
    com.action.UserAction.setUser(UserAction.java:29)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:601)
    org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:269)
    org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:170)
    org.apache.struts.chain.commands.servlet.ExecuteAction.execute(ExecuteAction.java:58)
    org.apache.struts.chain.commands.AbstractExecuteAction.execute(AbstractExecuteAction.java:67)
    org.apache.struts.chain.commands.ActionCommandBase.execute(ActionCommandBase.java:51)
    org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:191)
    org.apache.commons.chain.generic.LookupCommand.execute(LookupCommand.java:305)
    org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:191)
    org.apache.struts.chain.ComposableRequestProcessor.process(ComposableRequestProcessor.java:283)
    org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
    org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:462)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

Config file: hibernate.cfg.xml

配置文件: hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.bytecode.use_reflection_optimizer">false</property>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/LIBRARY</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">G190419g</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.show_sql">false</property>
        <property name="hibernate.current_session_context_class">thread</property>
        <property name="hibernate.cglib.use_reflection_optimizer">false</property>
        <property name="hibernate.c3p0.acquire_increment">1</property>
        <property name="hibernate.c3p0.idle_test_period">3000</property>
        <property name="hibernate.c3p0.max_size">100</property>
        <property name="hibernate.c3p0.max_statements">50</property>
        <property name="hibernate.c3p0.min_size">10</property>
        <property name="hibernate.c3p0.timeout">100</property>
        <property name="hibernate.c3p0.testConnectionOnCheckout">true</property>
        <mapping resource="com/petrez/model/Users.hbm.xml" />
    </session-factory>
</hibernate-configuration>

Mapping file: Users.hbm.xml

映射文件: Users.hbm.xml

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.petrez.model">
    <class name="Users" table="Users" >
        <id name="id" column="Id" type="java.lang.Integer" >
            <generator class="increment"/>
        </id>

        <property name="firstName" type="java.lang.String">
            <column name="FirstName" length="45" />
        </property>

        <property name="lastName" type="java.lang.String">
            <column name="LastName" length="45" />
        </property>

        <property name="eMail" type="java.lang.String">
            <column name="Email" length="45" />
        </property>
    </class>
</hibernate-mapping>

So anybody can say me where is here a mistake? Thanks.

所以任何人都可以说我这里有什么错误?谢谢。

UPD:Project Structure.

UPD:项目结构。

Project Structure

项目结构

Well. I didn't find a solution, but annotations helped me.

好。我没有找到解决方案,但注释帮助了我。

回答by CHowdappaM

Change your type of mappings from Java types to Hibernate types: For String i am giving as example:

将您的映射类型从 Java 类型更改为 Hibernate 类型:对于字符串,我举了一个例子:

 <property name="logMsg" type="string">
            <column name="LOG_MSG" length="35" />
        </property>