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

前端之家收集整理的这篇文章主要介绍了"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>

猜你在找的XML相关文章