Hi,
I kinda get the feeling I'm missing out on something ... I have to class (and two tables in my db), which have a one-to-many relation. So now I want to create a new order, and add that to the list of orders of an customer, so I tried this code:
Code:
Customer c = new Customer { Alter = 23, Name = "BigBoss" };
Order o = new Order {Amount=100, Date=new DateTime(2008, 8, 18)};
c.Orders.Add(o);
session.Save(c);
session.Flush();
session.Clear();
Customer fromDb = session.Get<Customer>(c.Id);
Assert.AreEqual(c.Name, fromDb.Name);
Assert.AreEqual(c.Orders.Count, fromDb.Orders.Count);
But my second assertion fails :( looking at the db-table I notice the customer-id is missing in the order.
I would assume, that nhibernate would take care of everything :(
and this are my mappings:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="SqlEngines" namespace="SqlEngines">
<class name="Customer">
<id name="Id">
<generator class="native"></generator>
</id>
<property name="Name"/>
<property name="Alter" column="Age"/>
<bag name="Orders" inverse="true" cascade="all">
<key >
<column name="CustomerId" not-null="true"/>
</key>
<one-to-many class="Order"/>
</bag>
</class>
<class name="Order" table="CustomerOrder">
<id name="Id">
<generator class="native" />
</id>
<property name="Date" column="OrderDate"/>
<property name="Amount"/>
<many-to-one name="MyCustomer" class="Customer" column="CustomerId" not-null="true" />
</class>
</hibernate-mapping>
BTW: I'm using .Net 3.5 and NHibernate 1.2.1.4000