1
votes

I'm starting to learn NHibernate (3.0) and picked up a copy of Packt Publishing's NHibernate 3.0 Cookbook.

There's a section on one-to-many mappings which I'm walking through but with my own database. It suggests I should do something like this to model a one to many relationship between customers and their domains:

public class Customer
{
  public virtual int Id { get; protected set; }
  public virtual string CustomerName { get; set; }
  // Customer has many domains
  public virtual IList<Domain> Domains { get; set; }
}

public class Domain
{
  public virtual int Id { get; protected set; }
  public virtual int CustomerID { get; set; }
  public virtual string DomainName { get; set; }
}

Customer Mapping:

<class name="Customer" table="tblCustomer">
  <id name="Id">
    <column name="CustomerID" sql-type="int" not-null="true"/>
    <generator class="identity"/>
  </id>

  <property name="CustomerName" column="Customer"/>
  <list name="Domains">
    <key column="CustomerID"/>
    <one-to-many class="Domain" />
  </list>
</class>

When I run this I get the following error:

XML validation error: The element 'list' in namespace 'urn:nhibernate-mapping-2.2' has invalid child element 'one-to-many' in namespace 'urn:nhibernate-mapping-2.2'. List of possible elements expected: 'index, list-index' in namespace 'urn:nhibernate-mapping-2.2'.

The book's example is a bit more complex in that they use table-per-subclass mappings:

alt text

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
    assembly="Eg.Core"
    namespace="Eg.Core">
  <subclass name="Movie" extends="Product">
    <property name="Director" />
    <list name="Actors" cascade="all-delete-orphan">
      <key column="MovieId" />
      <index column="ActorIndex" />
      <one-to-many class="ActorRole"/>  <-- Is this wrong?
    </list>
  </subclass>
</hibernate-mapping>

I'm guessing the book is wrong?

1
From the error message, one could easily assume that one-to-many can never be a child to list. I would call that a bug in the error message.Mitja

1 Answers

4
votes

No, your mapping is missing the index element. A list in NHibernate is an ordered set, if you want an unordered set use bag mapping.