I am extremely confused as to why this is failing, or why it should be failing. After much research I see that many others have these issues, yet very few answers.
Logically, I would feel that I should be able to do this: (Nhib 1.2.0)
assume session has been created, etc etc.
Code:
Orders o = new Orders();
OrderItem i = new OrderItem();
i.Order = o;
o.Items.Add(i);
session.SaveOrUpdate(o);
session.Flush();
No matter how I build my maps, changing cascade and inverse settings, I cannot get this to work without the transient object exception:
object references an unsaved transient instance - save the transient instance before flushing: BLL.Orders
For simplification, these would be the appropriate maps:
Code:
<class name="Orders" table="Orders">
<id name="ID" column="ID" type="Int64" unsaved-value="0">
<generator class="identity"/>
</id>
<bag name="Items" inverse="true" cascade="all-delete-orphan">
<key column="OrdersID" />
<one-to-many class="BLL.OrderItem, BLL" />
</bag>
</class>
<class name="OrderItem" table="PublisherPublicationFileSlot">
<id name="Id" column="ID" type="Int64" unsaved-value="0">
<generator class="identity"/>
</id>
<many-to-one name="Order" column="OrdersID" class="BLL.Orders, BLL" />
</class>
What I am attempting to do is, from where I sit, a perfectly reasonable expectation. The only examples I can find that are similar to my example here, load an already persisted parent class, then add to the collection, and save the parent again.
Does NHib not support this seemingly obvious behavior?