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

时间:2013-10-07 08:52:01

标签: java xml hibernate jpa

我在InvalidMappingException方法调用上org.hibernate.cfg.Configuration.configure()

堆栈跟踪:


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)

配置文件: 的 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>

映射文件: 的 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>

所以任何人都可以说我在哪里这是一个错误?感谢。

UPD:项目结构。

Project Structure

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

1 个答案:

答案 0 :(得分:1)

将您的映射类型从Java类型更改为Hibernate类型:对于String,我举例说明:

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