Hibernate one to many mapping works with a list but not a set?

一笑奈何 提交于 2019-12-02 09:44:02

问题


Sorry to bother - perhaps this is a very simple question - but for some reason the version below fails to get parsed, whereas the version with set works fine. In fact, if I just take the set version and replace set with list I get:

nested exception is org.hibernate.InvalidMappingException: Could not parse mapping document from invalid mapping

Thank you Misha

    <!-- bi-directional one-to-many association to SpreadsheetImportTemplateColumn -->
    <list name="columns">
<!--
    <set name="columns" lazy="false" inverse="true"
        cascade="all-delete-orphan" sort="natural"
        order-by="voided asc, preferred desc, date_created desc">
-->
        <key column="template_id" not-null="true" />
<!--
        <one-to-many class="SpreadsheetImportTemplateColumn" />
    </set>
-->
    </list>

回答1:


You said

whereas the version with set works fine

Here goes list DOCTYPE

<!ELEMENT list (
    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*
)>

Ass you can see, list element needs either index or list-index element, a key element, and one of the following

  • element
  • one-to-many
  • many-to-many
  • composite-element
  • many-to-any

Here goes list-index DOCTYPE

<!-- Declares the type and column mapping for a collection index (array or list index, or key of a map). -->

<!ELEMENT list-index (column?)>
<!ATTLIST list-index column CDATA #IMPLIED>
<!ATTLIST list-index base CDATA "0">

So you should use

<list name="columns">
    <key column="template_id" not-null="true"/>
    <list-index column="WHICH COLUMN SHOULD BE USED AS INDEX"/>
    <one-to-many class="SpreadsheetImportTemplateColumn" />
</list>

But if you want to use a list instead of a set and does not have a list-index column, you can use a bag instead. Initialize as follows

Collection<SpreadsheetImportTemplateColumn> columns = new ArrayList<SpreadsheetImportTemplateColumn>();

And define this mapping instead

<bag name="columns">
     <key column="template_id" not-null="true"/>
     <one-to-many class="SpreadsheetImportTemplateColumn"/>
</bag>



回答2:


In Hibernate, a List must specify an index column.

See Section 6.2.3 of the Hibernate documentation.



来源:https://stackoverflow.com/questions/3580686/hibernate-one-to-many-mapping-works-with-a-list-but-not-a-set

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!