roye wrote:
Not exactly... I should point out that i'm new to OO and .NET (i'm an oracle developer). My exact code is listed below what i'm trying to achieve is :
- Get a list of Companies from the database
- add a new meeting to Company with Id 1.
- Save all this back to the database
I think the code below should do this, but it doesn't it does what you say :
1. It executes successfully.
2. the Communcations table has NULL in the foreign key column to Company table.
Code
===
NHibernate.ICriteria compCriteria = session.CreateCriteria(typeof(Company));
foreach (Company c in compCriteria.List())
{
if (c.Id == 1)
{
newMeeting = new Meeting(4, "com 4", aDate, "com 4");
c.Communications.Add(newMeeting);
transaction = session.BeginTransaction();
if (c.Communications.Count != 0)
{
//save each instruments changed/new communications
foreach (Communication Cm in c.Communications)
{
session.Save(Cm);
}
}
session.Save(c);
transaction.Commit();
}
}
First of all: the session.Save(); means "INSERT" in SQL language (Save new instance). session.Update would save modified instance, but it is not needed if object is read trought the same session.
The only thing I do not understand - the database should have thrown Primary Key Violation in case of that code :O
EDIT: To get specific object from database, use session.Get(typeof(Company), 1);
or
session.Get<Company>(1);
Gert