I'm sure this problem has been addressed before but I can't find a 'duplicate'. So the question is:
I have a bidirectional one-to-many association: document and comments. The comments table has a foreign key (not nullable) pointing to documents table. The classes are:
public class Document {
public virtual Guid Id {get; set;}
public virtual string Title {get; set;}
public virtual string Body {get; set;}
public virtual IList<Comment> Comments {get; set;}
}
public class Comment {
public virtual Guid Id {get; set;}
public virtual Document Document {get; set;}
public virtual string Body {get; set;}
}
//Mappings
public class DocumentMap : ClassMap<Document> {
public DocumentMap(){
Id(x => x.Id).GeneratedBy.GuidComb();
Map(x=>x.Title);
Map(x=>x.Body);
HasMany(x=>x.Comments).Inverse().Cascade.All().AsBag();
}
}
public class CommentMap : ClassMap<Comment> {
public CommentMap(){
Id(x => x.Id).GeneratedBy.GuidComb();
Map(x=>Body);
References(x=>x.Document).Column("DocumentId").Not.Nullable();
}
}
//The problem
var existingDocument = FetchDocumentFromDatabase(someDocumentGuidId);
Console.WriteLine(existingDocument.Comments.Count()); //0
existingDocument.Comments.Add(new Comment { Document = existingDocument, Body = "Test"});
session.SaveOrUpdateCopy(existingDocument);//all OK
//commiting transaction etc
Console.WriteLine(existingDocument.Comments[0].Id);
// 00000000-0000-0000-0000-000000000000
But I need my new comment Id here!
The comment is added OK to DB but the object Id is still an empty Guid.
How do I automatically fill child object Ids after they were added as part of parent object update?
I'm using NHibernate 3.0.0 Alpha 3 (seems to be very stable) and Fluent NHibernate v.1.1.
Please advice.
UPDATE 1: all changes propage to DB just fine (document properties are updated as well as new comments are added).