2
votes

I have the following Fluent NHibernate configuration...

public class TemplateMap : ClassMap<Template>
{
    public TemplateMap()
    {
        Not.LazyLoad();
        Id(t => t.Id).GeneratedBy.Sequence("TEMPLATE_ID_SEQ");
        Map(t => t.Name);
        Component(t => t.Path, 
            p => p.HasMany(f => f.Fragments)
                .Access.CamelCaseField(Prefix.Underscore)
                .Cascade.AllDeleteOrphan()
                .KeyColumn("template_id")
            )
            .Access.CamelCaseField(Prefix.Underscore);
    }
}

Which results in the following HBM...

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-access="property" auto-import="true" default-cascade="none" default-lazy="true">
  <class xmlns="urn:nhibernate-mapping-2.2" lazy="false" name="TestApp.Domain.Template, TestApp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" table="`Template`">
    <id name="Id" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <column name="Id" />
      <generator class="sequence">
        <param name="sequence">TEMPLATE_ID_SEQ</param>
      </generator>
    </id>
    <component name="Path" insert="true" update="true" access="field.camelcase-underscore" optimistic-lock="true">
      <bag access="field.camelcase-underscore" cascade="all-delete-orphan" name="Fragments">
        <key>
          <column name="template_id" />
        </key>
        <one-to-many class="TestApp.Domain.Fragment, TestApp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
      </bag>
    </component>
    <property name="Name" type="System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <column name="Name" />
    </property>
  </class>
</hibernate-mapping>

To treat the collection as a list, I add the "AsList" method...

public class TemplateMap : ClassMap<Template>
{
    public TemplateMap()
    {
        Not.LazyLoad();
        Id(t => t.Id).GeneratedBy.Sequence("TEMPLATE_ID_SEQ");
        Map(t => t.Name);
        Component(t => t.Path, 
            p => p.HasMany(f => f.Fragments)
                .Access.CamelCaseField(Prefix.Underscore)
                .Cascade.AllDeleteOrphan()
                .KeyColumn("template_id")
                .AsList(index => index.Column("Position"))
            )
            .Access.CamelCaseField(Prefix.Underscore);
    }
}

Which results in the following HMB...

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-access="property" auto-import="true" default-cascade="none" default-lazy="true">
  <class xmlns="urn:nhibernate-mapping-2.2" lazy="false" name="TestApp.Domain.Template, TestApp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" table="`Template`">
    <id name="Id" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <column name="Id" />
      <generator class="sequence">
        <param name="sequence">TEMPLATE_ID_SEQ</param>
      </generator>
    </id>
    <component name="Path" insert="true" update="true" access="field.camelcase-underscore" optimistic-lock="true">
      <list access="field.camelcase-underscore" cascade="all-delete-orphan" name="Fragments">
        <index>
          <column name="Position" />
        </index>
        <key>
          <column name="template_id" />
        </key>
        <one-to-many class="TestApp.Domain.Fragment, TestApp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
      </list>
    </component>
    <property name="Name" type="System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <column name="Name" />
    </property>
  </class>
</hibernate-mapping>

However, whilst the first configuration (without "AsList") works OK, the second causes this runtime error:

The element 'list' in namespace 'urn:nhibernate-mapping-2.2' has invalid child element 'index' in namespace 'urn:nhibernate-mapping-2.2'. List of possible elements expected: 'meta, subselect, cache, synchronize, comment, key' in namespace 'urn:nhibernate-mapping-2.2'.

Any ideas?

A bit more detail about my domain model: a template has an id, name and path. A path is an ordered list of fragments.

Many thanks

Sandy

1

1 Answers

3
votes

NHibernate is right: Fluent is generating invalid XML (<key> should go before <index>). Check if you're using the latest version.

By the way, why are you using a component? You could just declare Path as IList<Fragment>.