views:

48

answers:

2

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>
A: 

In Hibernate, a List must specify an index column.

See Section 6.2.3 of the Hibernate documentation.

MJ Richardson
+4  A: 

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>
Arthur Ronald F D Garcia
Very exhaustive answer, +1
Pascal Thivent