Could not parse mapping document from invalid mapping

错误信息:

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
org.hibernate.InvalidMappingException: Could not parse mapping document from resource test/hibernate/Product.hbm.xml
at org.hibernate.cfg.Configuration.addResource(Configuration.java:575)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1593)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1561)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1540)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1514)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1434)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1420)
at test.hibernate.HibernateTest.main(HibernateTest.java:19)
Caused by: org.hibernate.InvalidMappingException: Could not parse mapping document from invalid mapping
at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:508)
at org.hibernate.cfg.Configuration.addResource(Configuration.java:572)
... 7 more
Caused by: org.xml.sax.SAXParseException: The content of element type "id" must match "(meta*,column*,type?,generator?)".
at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
at org.apache.xerces.util.ErrorHandlerWrapper.error(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.dtd.XMLDTDValidator.handleEndElement(Unknown Source)
at org.apache.xerces.impl.dtd.XMLDTDValidator.endElement(Unknown Source)
at org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanEndElement(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
at org.dom4j.io.SAXReader.read(SAXReader.java:465)
at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:505)
... 8 more

纠结了半天终于解决了 和大家分享一下出现这种情况的解决方案

 大家请看我的hibernate.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>
      <!-- 数据库URL -->
      <property name="connection.url">
        jdbc:mysql://localhost:3306/javaweb
      </property>
      <!-- 数据库驱动类 -->
      <property name="connection.driver_class">
        com.mysql.jdbc.Driver
      </property>
      <!-- 数据库用户名 -->
      <property name="connection.username">
        root
      </property>
      <!-- 数据库用户密码 -->
      <property name="connection.password">
        123
      </property>
      <!-- 数据库方言 -->
      <property name="dialect">
        org.hibernate.dialect.MySQLDialect
      </property>
      <!-- 指定映射文件 -->
      <mapping resource="com/xzit/hibernate/Product.hbm.xml"/>
    </session-factory>
  </hibernate-configuration>

 

再看一下我错误的Product.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-mapping>
  <class name="com.xzit.hibernate.Product">
    <id name="id">
      <generator class="assigned"></generator>
    </id>
   
    <property name="name"></property>
    <property name="price"></property>
  </class>
</hibernate-mapping> 

两者有颜色的区域是不是一模一样呢 对了 这就是问题所在

 

应该将Product.hbm.xml中的改成下面的

<!DOCTYPE hibernate-mapping PUBLIC
             "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
          "
http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值