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

一世执手 提交于 2019-12-02 03:22:53

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>

In Hibernate, a List must specify an index column.

See Section 6.2.3 of the Hibernate documentation.

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