我有一个条目,其中包含如下映射文件中所示的注释列表。
我的场景:
- 我为给定条目添加新注释(如 SaveComment 方法中所示)并提交更改
- 之后我查询入口域对象的评论,但我的新评论尚未添加
NHibnerate 在这里使用过时的缓存数据吗?我该如何解决这个问题?
using (ITransaction transaction = Session.BeginTransaction())
{
try
{
Entry entry = Session.Load<Entry>(message.EntryId);
Comment comment = SaveComment(entry, new BroadcastMetadata { some data });
transaction.Commit();
// I access the entry.LatestBroadcast info here
// but my entry doesnt have the new comment assigned yet !
var latestData = entry.LatestBroadcast; // is null
}
}
private Comment SaveComment(Entry entry, BroadcastMetadata broadcastMetadata)
{
Session.Save(broadcastMetadata);
var comment = new Comment
{
Entry = entry
OldBroadcastData = entry.LatestBroadcast,
NewBroadcastData = broadcastMetadata
};
Session.Save(comment);
return comment;
}
我的 C# 课程:
public class Entry
{
public virtual BroadcastMetadata LatestBroadcast
{
get
{
BroadcastMetadata latestBroadcast = null;
Comment broadcastComment = Comments.LastOrDefault();
if (broadcastComment != null)
{
latestBroadcast = broadcastComment.NewBroadcastData;
}
else
{
latestBroadcast = BroadcastData;
}
return latestBroadcast;
}
}
public virtual IList<Comment> Comments { get; protected set; }
}
我的映射:
<class name="Entry" table="`LogbookEntry`">
<bag name="Comments" table="LogbookComment" lazy="false" inverse="true">
<key column="EntryId" />
<one-to-many
class="LogbookService.Core.Model.Comment, LogbookService.Core" />
</bag>
<class name="Comment" table="LogbookComment">
<many-to-one name="Entry" column="EntryId" fetch="join" lazy="false" cascade="none"
class="LogbookService.Core.Model.Entry, LogbookService.Core" />
</class>