双向一对多关联插入后自动更新子集合 ID
我确信这个问题以前已经解决过,但我找不到“重复”。所以问题是:
我有一个双向一对多关联:文档和评论。 评论表有一个指向文档表的外键(不可为空)。 课程是:
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
但我需要我的新评论 ID!
注释已添加到 DB,但对象 Id 仍然是空 Guid。
作为父对象更新的一部分添加子对象 ID 后,如何自动填充子对象 ID?
我正在使用NHibernate 3.0.0 Alpha 3(似乎非常稳定)和Fluent NHibernate v.1.1。
请指教。
更新 1:所有更改都会很好地传播到数据库(更新文档属性并添加新注释)。
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).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是您正在使用
SaveOrUpdateCopy
。这里发生的情况是existingDocument
不用于持久化到数据库。来自 http://nhibernate.info/doc/nh/en/ index.html#manipulatedata-updating-detached:
这意味着
existingDocument
被保留,NHibernate 使用完全不同的实例来实际执行其操作。那我们能做什么呢。有几个选项:
SaveOrUpdateCopy
的结果应该是正确的Document
。您可以将代码更改为:您可以切换到
SaveOrUpdate
。现在,我猜有一个非常具体的原因,这对您来说可能是不可能的,因为您专门选择了SaveOrUpdateCopy
。无论如何,SaveOrUpdate
是您通常使用的;您可以重新加载
文档
。这意味着在SaveOrUpdateCopy
之后,您可以输入:如果您使用
SaveOrUpdateCopy
因为existingDocument
来自不同的ISession
,您还可以选择使用Lock()
将existingDocument
重新附加到当前ISession
。将existingDocument
“锁定”到新的ISession
后,您可以使用SaveOrUpdate
而不是SaveOrUpdateCopy
然后将使用该实例,包括Comment
并分配Guid
。The problem is that you're using
SaveOrUpdateCopy
. What happens here is thatexistingDocument
is not used to persist to the database.From http://nhibernate.info/doc/nh/en/index.html#manipulatingdata-updating-detached:
What this means is that
existingDocument
is left alone and NHibernate uses entirely different instances to actually perform its actions on.So what can we do. There are a few options:
The result from
SaveOrUpdateCopy
should be the correctDocument
. You can change your code to:You can switch to
SaveOrUpdate
. Now, I guess there is a very specific reason this may not be possible for you, because you specifically choseSaveOrUpdateCopy
. Anyway,SaveOrUpdate
is what you'd normally use;You can reload the
Document
. This means that after theSaveOrUpdateCopy
, you could put:If you are using
SaveOrUpdateCopy
becauseexistingDocument
came from a differentISession
, you can also choose to reattach theexistingDocument
to the currentISession
usingLock()
. After you've 'locked' in theexistingDocument
into the newISession
, you can useSaveOrUpdate
instead ofSaveOrUpdateCopy
and that instance, including theComment
will then be used and theGuid
will be assigned.