kubal5003
kubal5003

Reputation: 7254

NHibernate - why items attached to a saved object are not saved to the db

I have the following code:

var rootFolder = new KnowledgeFolder { Archived = true, Name = path.First()};
this._folderRepository.Save(rootFolder); // this goes to the db

And then later on: (irrelevant code is not here)

var child = new KnowledgeFolder { Name = entry, Archived = true };
rootFolder.Children.Add(child);
//////////
this._folderRepository.FlushSession();

rootFolder is correctly saved to the db, but other items that I later add to Children won't save. Why? I know that if root folder was previously downloaded from database this would work. Why NHibernate is not smart enough to check the object graph?

//edit

Mapping is done using fluent api like this:

mapping.HasManyBidirectional<KnowledgeFolder, KnowledgeFolder>(g => g.Children, p => p.Parent);

Upvotes: 0

Views: 85

Answers (1)

Fourth
Fourth

Reputation: 9351

You arent setting the parent on the child items so nhibernate isn't aware of both sides of the relationship.

var child = new KnowledgeFolder { Name = entry, Archived = true };
rootFolder.Children.Add(child);
child.Parent = rootFolder;

Upvotes: 1

Related Questions