Title: Nhibernate mapping clarification wrt Map using entities
Tags: nhibernate;nhibernate-mapping
Question: When using a mapping such as:

```<class name="Product"&gt;
  <id name="Id"&gt;
    <generator class="guid" /&gt;
  </id&gt;
  <property name="Name"/&gt;
  <map name="UserAddedFields" table="UserAddedFields" &gt;
    <key column="parent_id"/&gt;
    <index-many-to-many column="UserAddedFieldId" class="UserAddedField"/&gt;
    <element column="fieldValue"/&gt;
  </map&gt;
</class&gt;
```

And I wanted to add a new UserAddedField to an existing Product I need to first save the UserAddedField or I will get a TransientObjectException. The exception seems to imply I could set a cascade action that would make it autosave but nothing I've tried seems to work. Is this not possible?

///////////////////////////////////////////////

After the suggestion below I've changed the mapping to the following but I'm getting a StaleStateException:  Batch update returned unexpected row count from update; actual row count: 0; expected: 1

```      <class name="Product"&gt;
    <id name="Id"&gt;
      <generator class="guid" /&gt;
    </id&gt;
    <property name="Name"/&gt;
    <bag name="UserAddedFields" lazy="true" inverse="true" batch-size="25" cascade="all-delete-orphan"&gt;
      <key column="parentId" /&gt;
      <one-to-many class="UserAddedFieldSetting" /&gt;
    </bag&gt;
  </class&gt;
  <class name="UserAddedField" &gt;
    <id name="Id"&gt;
      <generator class="guid" /&gt;
    </id&gt;
    <property name="Name" /&gt;
    <property name="IsGlobal" type="bool"/&gt;
  </class&gt;
  <class name="UserAddedFieldSetting" &gt;
    <id name="Id"&gt;
      <generator class="guid" /&gt;
    </id&gt;
    <property name="FieldValue" /&gt;
    <many-to-one class="Product" name="Product" /&gt;
    <many-to-one class="UserAddedField" name="UserAddedField" cascade="all"/&gt;
  </class&gt;
```


Here is the accepted answer: As you've already experienced, cascading is not supported on the ```index``` element/mapping. While elements like


Map or
ManyToOne 


do support ```cascade```, the ```index-many-to-many``` is just another index, which nature is not intended to be cascade supporting.

My suggestion would be: do not use the map. Introduce brand new object instead, represented by table ```UserAddedFields```. If we'd extend it with a surrogated primary key, we can have an object like:

```public class UserAddedFieldSetting
{
   public virtual int Id { get; protected set; }

   public virtual Product Product { get; set; }
   public virtual UserAddedField UserAddedField { get; set; }
   public virtual decimal FieldValue { get; set; }
```

which can be mapped as a ```<bag&gt;```, i.e: ```IList<UserAddedFieldSetting&gt;```. Not only it will support cascading on the ```many-to-one``` mapping of the ```UserAddedField```, but also the querying will be much more simple and straightforward

```<bag name="UserAddedFields" lazy="true" inverse="true" 
     batch-size="25" 
     cascade="all-delete-orphan"&gt;
  <key column="parent_id" /&gt;
  <one-to-many class="UserAddedFieldSetting" /&gt;
</bag&gt;
```

EXTEND: 

So, now, we have a mapping (see updated part in the question), which is working as needed - well almost. The issue is, that NHibernate is executing the ```SaveOrUpdate()``` on our ```many-to-one``` relation. 

The problem here is, how to decide, that ```SaveOrUpdate``` should INSERT or UPDATE? And NHibernate decided to UPDATE by the way... but the row count returned was 0. Because there was in fact no row to update. We need to insert.

The key to the answer is in the mapping of the "unsaved value", the value representing the new == "to be inserted" state.

So let's extend the mapping:

```<class name="UserAddedField" &gt;
  <id name="Id" unsaved-value="00000000-0000-0000-0000-000000000000"&gt;
    <generator class="guid" /&gt;
  </id&gt;
  ...
```

And be sure, that every new C# instance of the ```UserAddedField``` has the 

```Id = Guid.Empty;
```

Now, NHibernate, when the SaveOrUpdate is called, will know, that ```Guid.Empty``` means do INSERT...

Comment for this answer: I've extended the answer, with description what is happening. We only need to learn NHibernate how to correctly decide if the `UserAddeField` is existing *(should be updated due to `cascade`)* or INSERTED
Comment for this answer: Thanks for your help, this is exactly what I want, but I still cant seem to get it to work. I've included the mapping I'm using in the original message. Any help on what I'm doing wrong would be greatly appreciated.
Comment for this answer: Excellent. Thanks, you nailed it. In my case however, setting unsaved-value was not needed. It was actually UserAddedFieldSetting that was causing the Update/Insert confusion. The mistake was in my test set up, When I created the new UserAddedFieldSetting object I foolishly assigned it an Id.


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2014:00:02/20/14', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<nhibernate><nhibernate-mapping>', 'Title': 'Nhibernate mapping clarification wrt Map using entities', 'ViewCount': 48, 'id': 21896323, 'user_AboutMe': None, 'user_AccountId': 493038.0, 'user_DisplayName': 'user1604008', 'user_Reputation': 939.0, 'user_Views': 49.0, 'user_id': 1604008.0}}