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

发布时间:2020-05-22 发布网站:脚本之家
脚本之家收集整理的这篇文章主要介绍了"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*,
loader?,
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>

总结

以上是脚本之家为你收集整理的"org.xml.sax.SAXParseException: The content of element type "class" must match" 异常解决全部内容,希望文章能够帮你解决"org.xml.sax.SAXParseException: The content of element type "class" must match" 异常解决所遇到的程序开发问题。

如果觉得脚本之家网站内容还不错,欢迎将脚本之家网站推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您喜欢交流学习经验,点击链接加入脚本之家官方QQ群:1065694478