当前位置: 首页 > 知识库问答 >
问题:

Hibernate4。映射异常无效。无法读取XML。必须声明元素类型hibernate映射

徐杰
2023-03-14

我在组织上有InvalidMappingException。冬眠cfg公司。配置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>

映射文件:用户。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:项目结构。

嗯。我没有找到解决办法,但是注释帮助了我。

共有1个答案

伍捷
2023-03-14

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

 <property name="logMsg" type="string">
            <column name="LOG_MSG" length="35" />
        </property> 
 类似资料:
  • 问题内容: 我正在尝试配置我的实体,但是hibernate会引发以下异常: 这些是我的实体: 下一个: 最后一个: Hibernate无法确定位于SDUser实体中的productAccess列的类型。我是Hibernate的新手,我不知道发生了什么。 我应该提供某种ID吗? 谢谢!! 问题答案: 在中,您需要在上添加关联信息:

  • 我试图通过使用多个测试数据在Cucumber DataTable中使用映射来自动化一个场景。在本测试中,我们将向测试步骤传递两次Username和Password。所以我们的测试应该输入用户名和密码一次,点击登录按钮,并重复相同的步骤再次。 我已经尝试使用for循环来使用Maps集合重复测试。1.我收到一个错误,表示类型不匹配:无法从元素类型2。当我将其转换为时,我得到另一个错误,说明方法send

  • 问题内容: 我试图配置我的实体,但是休眠抛出以下异常: 这些是我的实体: 下一个: 最后一个: Hibernate无法确定位于SDUser实体中的productAccess列的类型。我是Hibernate的新手,我不知道发生了什么。 我应该提供某种ID吗? 谢谢!! 问题答案: 在中,您需要在上添加关联信息:

  • 是否强制将我的外键实体从ClassA映射到ClassB中的主实体?

  • 我认为错误并不在注释中,因为我更改了几次注释,仍然得到了相同的异常。

  • maptask:开始刷新映射输出 2017-04-20 20:53:20,101 WARN[main]org.apache.hadoop.mapred.yarnchild:异常运行子级:java.lang.nullPointerException位于org.apache.parquet.avro.avroschemaconverter.convertField(avroschemaconverte