"org.xml.sax.SAXParseException: The content of element type "class" must match" 异常解决

异常信息:


Caused by: org.xml.sax.SAXParseException: The content of element type "class" must match"(meta*,subselect?,cache?,synchronize*,comment?,tuplizer*,(id|composite-id),discriminator?,natural-id?,(version|timestamp)?,(property|many-to-one|one-to-one|component|dynamic-component|properties|any|map|set|list|bag|idbag|array|primitive-array)*,((join*,subclass*)|joined-subclass*|union-subclass*),loader?,sql-insert?,sql-update?,sql-delete?,filter*,resultset*,(query|sql-query)*)".
 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.dtd.XMLDTDValidator.handleEndElement(XMLDTDValidator.java:2017)
 at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.endElement(XMLDTDValidator.java:901)
 at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanEndElement(XMLDocumentFragmentScannerImpl.java:1774)
 at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2930)
 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:460)
 ... 21 more

 

 

配置文件内容:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC 
 "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping 
 package="com.red.bean">
 <class name="TItem" table="t_titem">
     <id name="id" column="id" type="integer">
        <generator class="increment"></generator>
     </id>
  
  <property name="name" 
    column="name" 
    type="string"/>

     <discriminator column="category" type="string"/>         
    
     <property name="manufacturer" 
              column="manufacturer"
              type="string"/>
              
              
     <subclass name="TDVD" discriminator-value="2">
         <property name="regionCode"
                  column="regionCode"
                  type="string"/>
     </subclass>       
     
     <subclass name="TBook" discriminator-value="1">
         <property name="pageCount"
                  column="pageCount"
                  type="string"/>
     </subclass>    
 </class>
</hibernate-mapping>


 

原因分析:

   在错误信息中有如下内容:

   <!ELEMENT class (
   meta*,
   subselect?,
   cache?,
   synchronize*,
   comment?,
   tuplizer*,
   (id|composite-id),
   discriminator?,
   natural-id?,
   (version|timestamp)?,
   (property|many-to-one|one-to-one|component|dynamic-component|properties|any|map|set|list|bag|idbag|array|primitive-array)*,
   ((join*,subclass*)|joined-subclass*|union-subclass*),
   loader?,sql-insert?,sql-update?,sql-delete?,
   filter*,
   resultset*,
   (query|sql-query)*
   )>
  class元素中的子元素必须按照上面定义的顺序排列,否则hibernate在解析*.hbm.xml文件时,就会报上面的错误。
 
   所以,只需要把discriminator放在id之后,property之前就可以了,正确的配置文件应该如下:
 
 
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC 
 "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping 
 package="com.red.bean">
 <class name="TItem" table="t_titem">
     <id name="id" column="id" type="integer">
        <generator class="increment"></generator>
     </id>
     
   <discriminator column="category" type="string"/>  

  <property name="name" 
    column="name" 
    type="string"/>       
    
     <property name="manufacturer" 
              column="manufacturer"
              type="string"/>
              
              
     <subclass name="TDVD" discriminator-value="2">
         <property name="regionCode"
                  column="regionCode"
                  type="string"/>
     </subclass>       
     
     <subclass name="TBook" discriminator-value="1">
         <property name="pageCount"
                  column="pageCount"
                  type="string"/>
     </subclass>    
 </class>
</hibernate-mapping>


 

 

 

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:大白 设计师:CSDN官方博客 返回首页
评论

打赏作者

pursuer211

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值