Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" schema="dbo" assembly="IWaiter.Model" namespace="IWaiter.Model">
<class name="CustomerOrder" table="CustomerOrder">
<id name="Id" column="Id" type="Int32" unsaved-value="0">
<generator class="identity"/>
</id>
<property name="DateCreated" column="DateCreated" type="DateTime" not-null="true" />
<many-to-one name="CustomerTable" column="CustomerTableId" not-null="true" />
<list name="LineItems" table="LineItem" lazy="true" >
<key column="OrderId"/>
<index column="LineNumber"/>
<composite-element class="LineItem" >
<property name="Quantity" column="Quantity" type="Int32" not-null="true"/>
<many-to-one name="Product" column="ProductId" not-null="true"/>
</composite-element>
</list>
</class>
</hibernate-mapping>
This is the mapping of CustomerOrder. The problem lies with the LineItems collection.
If I load an order from the database, the collection gets populated correctly. If I try to save an order .... that's where the problem appears. The order is saved but the collection is not saved.
Code:
CustomerOrder order = new CustomerOrder ( );
order.DateCreated = DateTime.Now;
order.CustomerTable = new CustomerTableDao ( ).GetById ( 1, false );
LineItem lineItem = new LineItem ( );
lineItem.Product = new ProductDao ( ).GetById ( 6, false );
lineItem.Quantity = 3;
order.LineItems = new List<LineItem> ( );
order.LineItems.Add ( lineItem );
new CustomerOrderDao ( new CustomerTableDao ( ) ).Save ( order );
new CustomerOrderDao ( new CustomerTableDao ( ) ).NHSession.Clear ( );
order = new CustomerOrderDao ( new CustomerTableDao ( ) ).GetById ( order.Id, false );
Assert.IsTrue ( order.LineItems.Count > 0 );
Any thoughts ?