开发者

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

开发者 https://www.devze.com 2023-01-13 02:04 出处:网络
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 a

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 c开发者_高级运维olumn="template_id" not-null="true" />
<!--
        <one-to-many class="SpreadsheetImportTemplateColumn" />
    </set>
-->
    </list>


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消