hibernate一对多列表映射错误

时间:2011-08-11 21:34:24

标签: xml hibernate mapping

我有以下hibernate xml映射文件段。

   

    <list name="networks" cascade="all">
                <key column="parent_id"/>    

                <one-to-many class="Network"/>
        </list>

它产生了以下异常,为什么?它确实有上述元素。

Caused by: org.xml.sax.SAXParseException: The content of element type "list" must match "(meta*,subselect?,cache?,synchronize*,comment?,key,(index|list-index),(element|one-to-many|many-to-many|composite-element|many-to-any),loader?,sql-insert?,sql-update?,sql-delete?,sql-delete-all?,filter*)".
    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.Configuratio

2 个答案:

答案 0 :(得分:8)

<list-index映射必须添加<List >

<list name="networks" cascade="all">
    <key column="parent_id"/>    
    <list-index column="order" base="0" />
    <one-to-many class="Network"/>
</list>

Hibernate参考文档

  

6.2.3. Indexed collections
  除了具有set和bag语义的集合映射之外,所有集合映射都需要一个索引列   收集表。索引列是映射到数组索引或列表索引的列   地图键。

答案 1 :(得分:2)

如果您不关心订单,可以使用包

<bag name="networks" cascade="all">
     <key column="parent_id"/>
     <one-to-many class="Network"/>
</bag>