2

I'm trying to learn NHibernate mapping by code by translating the examples in 'NHibernate in Action' and I'm having a problem with one of the examples. I've inlcuded the the XML that I'm trying to translate and my effort at translating it. The code basically has a many to many relationship between a category and an item. It uses a component class named CategorizedItem to maintain the relationship between the two entities.

The problem that I'm having is that once I declare the Items property of the Category in the lambda, the Item Class's properties are the only ones available for selection in the component mapping. I'm wanting to let the mapping know that the CategorizedItem's properties are those to be mapped in the relationship, but I'm at a loss as to how this is achieved. Below is the XML that I'm trying to translate...

<set name="Items" table="CATEGORY_ITEM" lazy="true" >
  <key column="CATEGORY_ID"/>
  <composite-element class="CategorizedItem">
    <parent name="Category"/>
    <many-to-one name="Item" class="Item" column="ITEM_ID" not-null="true"/>
    <many-to-one name="User" class="User" column="USER_ID" not-null="true"/>
    <property name="DateAdded" column="DATE_ADDED" not-null="true"/>
  </composite-element>
</set>

... and here is the mapping that I've partially done.

Set(
    category => category.Items,
    map =>
    {
      map.Key(key =>
        {
          key.Column("CATEGORY_ID");

        });
      map.Table("CATEGORY_ITEM");
      map.Lazy(CollectionLazy.Lazy);
    },
    r => r.Component(m =>
    {
      m.Class

    })
  );

The class property of the component looks like the likely target, but it is rejecting CategorizedItem as the type parameter. Any help would be greatly appreciated.

MickySmig
  • 239
  • 1
  • 6
  • 17
  • 1
    What means "rejecting CategorizedItem"? compiler error, runtime error, what error message? Show the complete code you tried. – Stefan Steinegger Oct 01 '12 at 12:46
  • Hi @Stefan Steinegger, I could not give you an error message, as I hadn't gotten to the point were I could run the code. – MickySmig Oct 01 '12 at 16:10

1 Answers1

3

You're almost there - in the call to component you need to set up how the properties of your component map to the relationship table, ie:

        Set(
            category => category.Items,
            map =>
                {
                    map.Key(key =>
                                {
                                    key.Column("CATEGORY_ID");

                                });
                    map.Table("CATEGORY_ITEM");
                    map.Lazy(CollectionLazy.Lazy);
                },
            r => r.Component(
                m =>
                    {
                        m.Parent(ci => ci.Parent);
                        m.ManyToOne(ci => ci.Item, r2 => r2.Column("ITEM_ID"));
                        m.ManyToOne(ci => ci.User, r2 => r2.Column("USER_ID"));
                        m.Property(ci => ci.DateAdded);
                    })
            );
Martin Ernst
  • 5,629
  • 2
  • 17
  • 14
  • Thanks @Martin Ernst, I must admit that I'd changed my code to make the relationship component an entity in its own right, but I'll now change it back to use yours. Many thanks. – MickySmig Oct 01 '12 at 16:02